solve one issue because of thread safe httpclient

I was using HttpClient to handle the http request and response. Previously I always debug on latest phones which are upper 4.0 everything works fine. One day I tried on my older phone Moto Defy running 2.3.7 from Cyanogenmod . There was one issue when I tried to post an image .

I got the log :

Invalid use of SingleClientConnManager: connection still allocated.

or

Caused by: java.lang.IllegalStateException: No wrapped connection.

or

Caused by: java.lang.IllegalStateException: Adapter is detached.

Searched in stackoverflow normally it is because of accessing single instance of HttpClient in different thread or did not close InputStream of httpresponse. 

Mine issue is the first one. 

So I learnt one solution to get a thread safe httpclient.

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;

}

Comments

Popular posts from this blog

A wired issue of MediaPlayer on Android

Problem when using Jackson json parser in Android.

Gradle issue - peer not authenticated