Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated
You need to consume the response body before you can reuse the connection for another request. You should not only read the response status, but read the response InputStream
fully to the last byte whereby you just ignore the read bytes.
Invalid use of SingleClientConnManager: connection still allocated
Invalid use of SingleClientConnManager: connection still allocated.
You are executing the http request two times that is completely wrong before you consume it. So remove the second httpclient.execute(httppost);
because you have already execute this http request.
and call this
httpResponse.getEntity().consumeContent();
Above method is called to indicate that the content of this entity is no longer required. All entity implementations are expected to release all allocated resources as a result of this method invocation
Android: Invalid use of SingleClientConnManager: connection still allocated
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());
Invalid use of SingleClientConnManager occurs in Android 2.2,2.3 but not in 4.1
The behavior of AsyncTask
changed on Android 3.2: if your targetSdkVersion
is 13 or higher, your AsyncTasks
will share a single background thread by default, and only one task will run at a time. Otherwise, your AsyncTasks
will use a thread pool and can run in parallel. Since HttpClient
is not thread-safe by default, that is why you are encountering a difference in behavior.
If you want to use HttpClient
across multiple threads, use a ThreadSafeClientConnManager
.
For more on the AsyncTask
behavior change: http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html
basichttpclient execute throws SingleClientConnManager: connection still allocated why?
SingleClientConnectionManager
is fully thread safe in the sense that when used by multiple execution threads its internal state is synchronized and is always consistent. This does not change the fact that it can dispense a single connection only. So, if two threads attempt to lease a connection, only one can succeed, while the other is likely to get 'java.lang.IllegalStateException: Invalid use of SingleClientConnManager'
You should be using a pooling connection manager if your application needs to execute requests concurrently.
Invalid use of BasicClientConnManager: connection still allocated
Is there anything wrong I am doing here?
Yes. As stated in the docs:
BasicClientConnectionManager is a simple connection manager that
maintains only one connection at a time. Even though this class is
thread-safe it ought to be used by one execution thread only.
BasicClientConnectionManager will make an effort to reuse the
connection for subsequent requests with the same route. It will,
however, close the existing connection and re-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, then java.lang.IllegalStateException is thrown.BasicClientConnectionManager is used by HttpClient per default.
See "Multithreaded request execution" on how to use a pooling connection manager that can handle requests across multiple threads.
Related Topics
How to Mock Java.Time.Localdate.Now()
Spring Cron Expression for Every Day 1:01:Am
Possible to Use Two Java Classes with Same Name and Same Package
When Does the Main Thread Stop in Java
CSV File with "Id" as First Item Is Corrupt in Excel
How to Create New Xml File from Existing Database in Postgresql Database Using Java
Tablecellrenderer and How to Refresh Cell Background Without Using Jtable.Repaint()
Execute Jar File with Multiple Classpath Libraries from Command Prompt
Get All Variable Names in a Class
Advantage of Set and Get Methods VS Public Variable
How to Create a New Packaging Type for Maven
Extracting Pairs of Words Using String.Split()
Why I'm Not Able to Unwrap and Serialize a Java Map Using the Jackson Java Library
Why Does Collections.Sort Use Mergesort But Arrays.Sort Does Not