服务器如何在不关闭的情况下从客户端获取多个请求?

时间:2013-10-04 14:06:35

标签: java android json

我已经实现了一个非常简单的Java服务器,它基本上侦听端口8080上的所有请求并将它们写入控制台。

我的代码工作正常,一次。但是对于每个服务器重启,它只需要一个请求。

服务器代码:

public class RunServer {

    public static void main(String[] args) {
        final int port = 8080;
        final String path = "/android";
        try {
            HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);     //Create the server to listen for incoming requests on the specified port
            server.createContext(path, new HttpHandler() { //Set a handler for     incoming requests to the specific path
                @Override
                public void handle(HttpExchange arg0) throws IOException {     //This method is called when new requests are incoming
                            InputStreamReader isr = new     InputStreamReader(arg0.getRequestBody(),"utf-8");
                            BufferedReader br = new BufferedReader(isr);
                            String query = br.readLine();

                            System.out.print(query);
                } });           
            server.setExecutor(null); //Default executor. Other executors may be     used to e.g. use a threadpool for handling concurrent incoming requests.
            server.start(); //Start the server.
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
}

我正在使用此客户端发布到我的服务器:

public class MyAsyncTask extends AsyncTask<String, String, Void>{
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://192.168.1.5:8080/android");
    String s;
    @Override
    protected Void doInBackground(String... params) {
        // Building post parameters
        // key and value pair
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
        nameValuePair.add(new BasicNameValuePair("message",
                "Hi, trying Android HTTP post!"));

        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }

        // Making HTTP Request
        try {
            HttpResponse response = httpClient.execute(httpPost);

            // writing response to log
            Log.d("Http Response:", response.toString());
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();

        }
        return null;
    }

}

1 个答案:

答案 0 :(得分:1)

我首先要分解问题。您需要找到(或构建)可以解决这些问题的组件:

服务器端

  • 侦听网络连接
  • 从网络流中读取数据
  • 解析JSON数据
  • 将JSON处理为有用的格式[可选]
  • 将数据存储在磁盘上
  • 从磁盘读取数据
  • 通过网络将数据发送回客户端

Android方

  • 通过网络连接服务器
  • 发送JSON数据
  • 从服务器请求数据

有很多与这些相关的问题。如果您对其中一个问题有疑问,请将其作为单独的问题发布。