Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Invalid use of SingleClientConnManager: connection still allocated [duplicate]

Possible Duplicate:
Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated

I am working in Android. I created HttpSingleton class to create single intance of HttpClient in my complete application.

This is my code to use this class:-

HttpGet get = new HttpGet("url/dologin/savitagupta/savitagupta");
**HttpResponse rp = HttpSigleton.getInstance().execute(get);**          
if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
   // some code here
}

and this is my class for single instance

public class HttpSigleton {
  private static HttpClient instance = null;
  protected HttpSigleton() {

  }
  public static HttpClient getInstance() {
    if(instance == null) {
       instance =new DefaultHttpClient();
    }
    return instance;
 }
}

Then error is occurred is :-

SingleClientConnManager : Invalid use of SingleClientConnManager: connection still allocated. Make sure to release the connection before allocating another one. Please suggest me what mistake i have done. I really need your help. Thank you in advance.

like image 921
Pushpendra Kuntal Avatar asked Feb 17 '12 06:02

Pushpendra Kuntal


2 Answers

For Android:

If you are not interested in the content, the cheapest way to get rid of your connection and avoid the error "connection still allocated" is:

httpResponse.getEntity().consumeContent();

See http://developer.android.com/reference/org/apache/http/HttpEntity.html#consumeContent()

like image 149
Carl D'Halluin Avatar answered Nov 15 '22 18:11

Carl D'Halluin


After calling:

HttpResponse rp = HttpSigleton.getInstance().execute(get);

Please make sure you make a call to either:

String html = EntityUtils.toString(rp.getEntity() /*, Encoding */);

or

EntityUtils.consume(rp.getEntity());
like image 38
Tinclon Avatar answered Nov 15 '22 18:11

Tinclon