java - Android - Java 服务器和 android 客户端

标签 java android

我已经实现了一个非常非常简单的 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;
    }

}

最佳答案

最好使用类似 Jetty 的东西或 Tomcat如果您要进行任何稍微严肃的 Java HTTP 服务。

如果您要坚持使用 HttpServer,但实际上并不真正了解它,我怀疑这与 HttpServer 的默认配置中的单线程性质有关,并且套接字保持打开状态并等待一段时间时间之后。也可能是您没有正确关闭 Android 客户端中的 HttpClient,因此它保持与服务器的连接打开,因此服务器等待该客户端。

关于java - Android - Java 服务器和 android 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191977/

相关文章:

java - 如何更改记录的时间戳?

java - SharedPreferences——新应用程序的初始值从何而来?

android - 获取当前屏幕亮度

java - Android EditText 颜色更改会破坏边距

android - 如何使用语音交互打开 Activity ?

Android 布局、表格、相对布局和线性布局,我对它们的差异感到困惑

c# - java中通过JNA访问.net dll

java - 将新键放入 HashMap 中会替换现有的不同键

java - 如何在 Eclipse 中正确导入这个库?

android - 如何从 Android 的文件对话框中获取所选文件的扩展名?