English 中文(简体)
Android作为Web服务服务器?
原标题:Android as Webservice Server?

也许与其他人相反,我正在考虑在Android上运行一个Web服务服务器。是否有支持此功能的库?我认为ksoap2例如只用于消费网络服务,而不是为它们服务,对吧?

而且,如果没有冗长的编码,这是不可能的,我只需要在Android上运行一个HTTP服务器,并用它接收二进制文件(通过POST)。

有人能给点提示吗?

Cheers, Marc

最佳回答

最后,我打消了在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");
    }
}
}
问题回答

http服务器android的第一个结果:http://code.google.com/p/i-jetty/

您基本上可以做到这一点,但如果没有root,您将无法绑定特权端口(如端口80),也无法设置OOM杀手值来优先保护您的服务器,而不是其他可能需要内存的东西。

当然,除非上游wifi或3g提供商愿意给你一个可供感兴趣的客户路由的IP地址,否则你将无能为力。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签