httpPost.releaseConnection(); 該代碼只會復用連接,並不會關閉當前連接,其實下面的方法
client.getConnectionManager().closeIdleConnections(0, TimeUnit.MICROSECONDS); 在某些時候也沒關閉,感覺和
httpPost.releaseConnection();
只是起到同樣復用的結果,兩個都寫更加保障(在某些版本中兩個方法只會有其中一個)
httpclient 4種關閉連接 參考這里面的4種方法 (個人未試過) 第三種方法貌似可以
在android4.2中 client.getConnectionManager().closeIdleConnections(0, TimeUnit.MICROSECONDS);
在httpclient 4.2 種
client.getConnectionManager().closeIdleConnections(0, TimeUnit.MICROSECONDS);
經測試 ,上面兩種調用也無用
client.getConnectionManager().closeIdleConnections(0, TimeUnit.MICROSECONDS);
在上面加上
httpGet.abort();
就再也沒有出現該錯了
代碼
String result = null;
HttpPost httpPost = new HttpPost(serverUrl);
try {
StringEntity e = new StringEntity(bodyStr,"UTF-8");
httpPost.setEntity(e);
httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
HttpResponse response = this.client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} catch (Exception var10) {
LogL.e(var10.getMessage());
result = null;
}finally {
httpPost.abort(); //終止
client.getConnectionManager().closeIdleConnections(0, TimeUnit.MICROSECONDS); //再次調用確保回收
}
return result;
終極方法
client = new .... (重新實例化client)