2013-04-18 25 views
1

我在使用basic-http-client在我的longpolling庫SignalA中。 basic-http-client基於HttpUrlConnection。在模擬器中運行時,可以打開一個長時間運行的請求,並執行另一個同時的請求。 在真實設備上運行我的代碼時,第二個POST會掛起,直到第一個完成。它掛在getResponseCode函數上。第二個HttpUrlConnection掛在真實設備上

在模擬器或真實設備上運行有什麼區別? 如何啓用多個AND同時請求?

+0

使用執行程序。 http://developer.android.com/reference/java/util/concurrent/Executor.html – Raghunandan

回答

1

從類似的問題,引用的解決方案問here: -

當它保持連接活着被打破,使得它試圖使用已被服務器關閉連接使用HttpURLConnection連接池。默認情況下,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());