开发者

Android as Webservice Server?

maybe contrary to others, I´m thinking of running a webservice Server on Android. Is there any Library that supports this? I think ksoap2 e.g. is only for consuming webservices, not for servin开发者_开发知识库g them, right?

And, if thats not possible without lengthy coding, I just need to run a HTTP Server on Android and receive binaries with it (via POST).

Can anybody give some hints?

Cheers, Marc


Finally, I´ve dismissed the thought of implementing it with a Webservice on Android-side and just managed to do it via Socket-Communication and a self-designed protocol, quite like this:

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");
    }
}
}


First result of http server android: http://code.google.com/p/i-jetty/


You can basically do it, but without root you won't be able to bind a privileged port such as port 80, or set an OOM killer value to prioritize preserving your server over other things that may want memory.

And of course you won't be able to do much unless the upstream wifi or 3g provider is willing to give you an IP address that's routable by the clients of interest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜