Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve error "Invalid use of BasicClientConnManager : Make sure to release the connection before allocating another one" in Android?

Here I am trying to POST some data from mobile to the server.For that First I have to login for authentication on the server. then after I am sending the data via POST request I have checked this related stackoverflow question. HttpClient 4.0.1 - how to release connection?

I have tried all these methods one by one.

1.EntityUtils.consume(entity);

2.is.close();

  1. response.getEntity().consumeContent(); //in that consumeContent() shows to be Deprecated

  2. post.abort();

Here is my detailed code in the pastebin links please check this.

Here are the program flow.

  1. First login is called Check HERE MY LOGIN REQUEST

  2. After login and before post idea THIS method is called.

  3. And finally post idea method is called.

    but logcat is showing me error :

    java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.

    Make sure to release the connection before allocating another one

W/System.err( 2217): java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection still allocated.
W/System.err( 2217): Make sure to release the connection before allocating another one.
W/System.err( 2217):  at ch.boye.httpclientandroidlib.impl.conn.BasicClientConnectionManager.getConnection(BasicClientConnectionManager.java:161)
W/System.err( 2217):  at ch.boye.httpclientandroidlib.impl.conn.BasicClientConnectionManager$1.getConnection(BasicClientConnectionManager.java:138)
W/System.err( 2217):  at ch.boye.httpclientandroidlib.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:455)
W/System.err( 2217):  at ch.boye.httpclientandroidlib.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:902)
W/System.err( 2217):  at ch.boye.httpclientandroidlib.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:801)
W/System.err( 2217):  at ch.boye.httpclientandroidlib.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:780)
W/System.err( 2217):  at com.rahul.cheerfoolz.api.util.postData(util.java:436)
W/System.err( 2217):  at com.rahul.cheerfoolz.postidea.idea.post_myidea_edit$Post_Idea.doInBackground(post_myidea_edit.java:354)
W/System.err( 2217):  at com.rahul.cheerfoolz.postidea.idea.post_myidea_edit$Post_Idea.doInBackground(post_myidea_edit.java:1)
W/System.err( 2217):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
W/System.err( 2217):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
W/System.err( 2217):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
W/System.err( 2217):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
W/System.err( 2217):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
W/System.err( 2217):  at java.lang.Thread.run(Thread.java:1019)
like image 403
Rahul Patel Avatar asked Oct 09 '12 11:10

Rahul Patel


1 Answers

When you create a DefaultHttpClient with specify any ClientConnectionManager it will create by default a BasicClientConnectionManager and if you check the javadoc says:

A connection manager for a single connection. This connection manager maintains only one active connection at a time. Even though this class is thread-safe it ought to be used by one execution thread only. BasicClientConnManager will make an effort to reuse the connection for subsequent requests with the same route. It will, however, close the existing connection and open it for the given route, if the route of the persistent connection does not match that of the connection request. If the connection has been already been allocated IllegalStateException is thrown.

Try setting a PoolingClientConnectionManager with custom configuration for your needs. See a simple example:

PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager( SchemeRegistryFactory.createDefault());
cxMgr.setMaxTotal(100);
cxMgr.setDefaultMaxPerRoute(20);

This is valid for HttpClient 4.2 since ThreadSafeClientConnManager was deprecated for that version. Hope this helps.

like image 109
Diego Marafetti Avatar answered Oct 14 '22 18:10

Diego Marafetti