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();  
就再也没有出现该错了


代码

  1. String result = null;
  2. HttpPost httpPost = new HttpPost(serverUrl);
  3. try {
  4. StringEntity e = new StringEntity(bodyStr,"UTF-8");
  5. httpPost.setEntity(e);
  6. httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
  7. HttpResponse response = this.client.execute(httpPost);
  8. if(response.getStatusLine().getStatusCode() == 200) {
  9. result = EntityUtils.toString(response.getEntity(), "utf-8");
  10. }
  11. } catch (Exception var10) {
  12. LogL.e(var10.getMessage());
  13. result = null;
  14. }finally {
  15. httpPost.abort(); //终止
  16. client.getConnectionManager().closeIdleConnections(0, TimeUnit.MICROSECONDS); //再次调用确保回收
  17. }
  18. return result;


终极方法

client  = new .... (重新实例化client)



05-06 05:22