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

Here I am trying to POST some data from mobile to server. To do this, I first need to log in to authenticate with the server. then after sending the data via a POST request, I checked this related stack question. HttpClient 4.0.1 - how to free a connection?

I tried all these methods one by one.

1.EntityUtils.consume (legal entity);

2.is.close ();

  1. .
  2. response.getEntity () consumeContent (); // that consumeContent () is displayed as deprecated

  3. post.abort ();

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

Here is the program stream.

  • The first login is called Check HERE MY REQUEST REQUEST

  • After logging in and before posting ideas, this method is called.

  • Finally, the post idea method is called.

    but logcat shows me an error:

    java.lang.IllegalStateException: Invalid use of BasicClientConnManager: connection is still highlighted.

    Be sure to release the connection before highlighting another

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) 
+4
source share
1 answer

When you create a DefaultHttpClient specifying any ClientConnectionManager, it will create a default BasicClientConnectionManager , and if you check that javadoc says:

Connection Manager for a single connection. This connection manager only supports one active connection at a time. Although this class is thread safe, it should be used by only one thread of execution. BasicClientConnManager will try to reuse the connection for subsequent requests with the same route. However, it closes the existing connection and opens it for this route if the permanent connection route does not match the connection request route. If the connection is already allocated, an IllegalStateException is thrown.

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

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

This is true for HttpClient 4.2 since ThreadSafeClientConnManager is deprecated for this version. Hope this helps.

+10
source

Source: https://habr.com/ru/post/1438633/


All Articles