也许与其他人相反,我正在考虑在Android上运行网络服务服务器。有图书馆支持吗?我认为ksoap2例如仅用于使用Web服务,而不用于提供Web服务,对吗?

并且,如果没有冗长的编码不可能实现,那么我只需要在Android上运行HTTP Server并接收二进制文件(通过POST)即可。

有人可以给些提示吗?

干杯,

最佳答案

最后,我放弃了在Android端使用Web服务来实现它的想法,而是设法通过Socket-Communication和自行设计的协议来实现它,就像这样:

public class AsyncTaskSocketServer extends AsyncTask<Integer, String, Integer> {

private int id;
private String TAG = "AsyncTaskSocketServer";

private AsyncTaskSocketServer() {
    super();
    Random generator = new Random();
    id = generator.nextInt();
    Log.d(TAG, "created with id: " + id);
}

@Override
protected Integer doInBackground(Integer... ports) {

    int port = ports[0];
    Log.v(TAG, "Trying to start on port: " + port + " with id: " + id);

    try {
        ServerSocket serverSocket = new ServerSocket(port);

        while (!isCancelled()) {
            Socket client = serverSocket.accept();
            try {
                Log.v(TAG, "Listening on port: "
                        + port);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(client.getInputStream()));
                String str = in.readLine();
                publishProgress(str);

            } catch (Exception e) {
                e.printStackTrace();
                Log.v(TAG, "Exception while socket.accept"+ id);
            } finally {
                client.close();
            }
            client.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.v(TAG, "Exception in SocketServer creation" + id);
    }
    return port;
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);
    String message = values[0];
    try {
        NetworkQueue.MESSAGE_IN_QUEUE.put(message);
        Log.v(TAG, "received: " + message);
    } catch (Exception e) {
        Logger.log("AsyncTaskSocketServer: Exception while writing to IN_QUEUE");
    }
}
}

07-24 09:46
查看更多