如何通过GET响应作为Android服务器发送数据

时间:2016-04-05 12:14:11

标签: android android-wifi tcp-ip

我需要将服务器作为android手机,模块连接到我并通过GET方法(TCP / IP连接)请求数据。 连接的部分很简单,我的问题是我应该如何处理这个GET方法?

class AsyncServerThread extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
            Log.e("ServerThread", "Connected");
        } catch (final IOException e) {
            Log.e("serverSocketExc", "?" + e.getMessage());
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "serverSocketExc: " +
                                    "?" + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            });
            e.printStackTrace();
        }
        Log.e("ServerThread", "listening for new connections");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "ServerThread: listening for new connections.",
                        Toast.LENGTH_LONG).show();
            }
        });
        while (shallContinue) {

            try {

                final Socket tempSocket = socket = serverSocket.accept();

                if (tempSocket.isConnected()) {
                    Log.e("socket", "accepted new Connection");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "accepted new Connection.",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                    /*CommunicationThread commThread = new CommunicationThread(socket);
                    new Thread(commThread).start();*/
                    Log.e("AsyncServerThread", "going to create client thread");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new AsyncClientThread().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, tempSocket);
                        }
                    });

                    out = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(socket.getOutputStream())),
                            true);
                } else {
                    Log.e("socket", "no new Connection");
                }

            } catch (IOException e) {
                Log.e("accept", "?" + e.getMessage());
                e.printStackTrace();
            }
        }
        Log.e("ServerThread", "ended listening for new connection");
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "ServerThread: ended listening for new connection.",
                        Toast.LENGTH_LONG).show();
            }
        });
        try {
            serverSocket.close();
        } catch (IOException e) {
            Log.e("close", "?" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:2)

HTTP 协议世界中可以找到 GET POST PUT 等等。<登记/>  但是您创建的服务器套接字不是http服务器(创建低级服务器套接字和http服务器套接字之间存在差异)。

所以我在几天之前就遇到了类似的情况,并使用了nanohttpd。 它是一个优雅的库,允许您在Android设备上运行微小的httpd Web服务器。

示例代码:

  public class HttpWebServer extends NanoHTTPD {

    int port;

    public HttpWebServer(int port) throws IOException {
        super(port);
        this.port = port;
    }

    public void startServer() throws IOException{

        start();
        Log.d(LOG_TAG, "visit http://localhost:" + port );

    }

    @Override
    public Response serve(IHTTPSession session) {


        Log.d(LOG_TAG, "Got http request.");

        //gets method type; GET, POST, PUT..etc
        Method method = ((HTTPSession) session).method


    }


}

如何添加依赖项?

dependencies {
    compile 'com.nanohttpd:nanohttpd-webserver:2.1.1'
}