2010-08-09 62 views
1

我使用ThreadSafeClientConnManager執行在Android後臺線程,建立與同時請求:Android ThreadSafeClientConnManager超時?

HttpParams params = new BasicHttpParams(); 
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
HttpProtocolParams.setUseExpectContinue(params, true); 

SchemeRegistry registry = new SchemeRegistry(); 
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 

ClientConnectionManager connman = new ThreadSafeClientConnManager(params, registry); 
client = new DefaultHttpClient(connman, params); 

和執行與要求(請注意,我使用OUTH-路標):

HttpGet request = new HttpGet("https://" + API_HOST + "/" + API_VERSION + path); 
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(key, secret); 
consumer.sign(request); 
HttpResponse response = client.execute(request); 

的問題是,一段時間後,我開始變得

java.net.SocketException: The operation timed out 

我需要做一些事情來顯式釋放後的連接要求?

回答

2

Apache的HttpClient的的使用指南,您需要確保消費上的任何彙集資源的所有內容,以保證其返回到池可用於其它線程以後 -

http://hc.apache.org/httpcomponents-core-4.0.1/tutorial/html/fundamentals.html#d0e244

如果底層庫引發異常,最好放棄您嘗試運行的HttpMethod,在這種情況下,連接將被終止。

+0

這幫了很多,謝謝! – 2010-08-10 18:09:15

+1

鏈接404,找不到! – 2012-08-19 19:44:07