Invalid use of SingleClientConnManager: connection still allocated
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)