Thiago
Thiago

Reputation: 13302

Android Invalid use of SingleClientConnManager: connection still allocated

Please note this question is unique for my HTTP call.

I have look around and some ppl say httpResponse.getEntity().consumeContent();

but I can use it, because I need to provide a return data.

I call my API doInBackground.... as return api.post("analytics", params);

How can I fix the invalid SingleClientConnManager into this http post :

public String post(String url, List<NameValuePair> params) {
    HttpPost request = new HttpPost(BASEURL + url); 
    HttpResponse response;

    try {
        request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        response = client.execute(request);
        HttpEntity entity = response.getEntity();            

        return EntityUtils.toString(entity);
    } 
    catch(ClientProtocolException e) {            
        return e.toString();
    } 
    catch(IOException e) {           
        return e.toString();
    }       
}//end post

This is the error I am getting :

03-12 12:33:46.926: V/Activity(28803): YourAsyncTask.class
03-12 12:33:46.926: W/Activity(28803): -------------------------------------------------
03-12 12:33:46.926: W/SingleClientConnManager(28803): Invalid use of SingleClientConnManager: connection still allocated.
03-12 12:33:46.926: W/SingleClientConnManager(28803): Make sure to release the connection before allocating another one.
03-12 12:33:46.926: I/System.out(28803): AsyncTask #4 calls detatch()
03-12 12:33:46.926: D/Toast(28803):  checkMirrorLinkEnabled returns : false
03-12 12:33:46.926: D/Toast(28803): showing allowed
03-12 12:33:46.926: W/AsyncTaskExecutor(28803): java.util.concurrent.ThreadPoolExecutor$Worker@433abe80[State = -1, empty queue] | AsyncTask # 5

Upvotes: 1

Views: 1019

Answers (1)

Thiago
Thiago

Reputation: 13302

After searching, I found this solution

HttpResponse httpResponse = httpClient.execute(httpPost);

//instead of httpClient use getThreadSafeClient() method.
HttpResponse httpResponse = getThreadSafeClient().execute(httpPost);

public static DefaultHttpClient getThreadSafeClient() {
        DefaultHttpClient client = new DefaultHttpClient();
        ClientConnectionManager mgr = client.getConnectionManager();
        HttpParams params = client.getParams();
    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
                mgr.getSchemeRegistry()), params);
   
        return client;
    }

Upvotes: 1

Related Questions