我正在使用 basic-http-client在我的长轮询库中 SignalA . basic-http-client 基于 HttpUrlConnection。在模拟器中运行时,是否可以打开一个长时间运行的后请求并同时执行另一个后请求。 在真实设备上运行我的代码时,第二个 POST 挂起,直到第一个完成。它卡在 getResponseCode 函数上。
在模拟器上运行和在真机上运行有什么区别? 如何启用多个 AND 同时请求?
最佳答案
引用类似问题的解决方案 here :-
HttpURLConnection
在保持连接 Activity 时使用的连接池已损坏,因此它会尝试使用已被服务器关闭的连接。默认情况下,Android 会在所有连接上设置 KeepAlive。
System.setProperty("http.keepAlive", "false");
是一种解决方法,它禁用所有连接的 KeepAlive,从而避免连接池中的错误。
conn.setRequestProperty("Connection","Keep-Alive");
为这个特定的连接打开 KeepAlive,本质上颠倒了 System.setProperty("http.keepAlive", "false");
确实如此。
此外,我总是明确地调用 connect(),因为它清楚地表明您要在哪里结束连接设置。我不确定调用此方法是否可选。
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setUseCaches(false);
conn.setRequestProperty("User-Agent", useragent);
conn.setConnectTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
consumer.sign(conn);
conn.connect();
InputSource is = new InputSource(conn.getInputStream());
关于android - 第二个 HttpUrlConnection 在真实设备上挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16083847/