Android Invalid use of SingleClientConnManager: connection still allocated

岁酱吖の 提交于 2019-12-08 03:23:50

问题


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

回答1:


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;
    }


来源:https://stackoverflow.com/questions/29001784/android-invalid-use-of-singleclientconnmanager-connection-still-allocated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!