Introduction to Android Exception details
This anomaly I encountered in the following two kinds of situations:
1. Java.lang.IllegalStateException:No wrapped connection.
2.java.lang.illegalstateexception:adapter is detached.
Reason:
1. Single-threaded execution of a request can be performed normally, and if multiple threads are used, a connection timeout occurs when multiple requests are executed simultaneously.
2.HttpConnection the concept of no connection pool, the number of requests to build a number of Io, in the event of a large number of access to the server IO may be exhausted.
3. This is usually because httpclient accesses a single instance of a different thread or does not close InputStream's HttpResponse.
solution: Get httpclient thread safety
Pre-resolved code:
Public httpclient httpclient = new Defaulthttpclient (); public void Postnoresult (final context, final string url, final map<string, string> maps, final string show)
{New Thread () {@Override public void run () {try {HttpPost post = new HttpPost (URL);
list<namevaluepair> params = new arraylist<namevaluepair> ();
For (String Key:maps.keySet ()) {Params.add (New Basicnamevaluepair (Key, Maps.get (key)); } post.setentity (New urlencodedformentity (params, HTTP.
Utf_8));
HttpResponse response = Httpclient.execute (post);//Error location if (Response.getstatusline (). Getstatuscode () = 200) {
Looper.prepare ();
String r = entityutils.tostring (Response.getentity ());
Toastutil.print_log (R);
if (show!= null) {toastutil.show;
} looper.loop ();}
catch (Unsupportedencodingexception e) {e.printstacktrace (); catch (Clientprotocolexception e) {E.printstaCktrace ();
catch (IOException e) {e.printstacktrace ();
}}}.start ();
}
After resolved code:
Public HttpClient httpclient = getthreadsafeclient ();//Get httpclient thread safety. public static Defaulthttpclient Getthreadsafeclient () {//obtain HttpClient Thread-safe method defaulthttpclient client = new Defaulthttp
Client ();
Clientconnectionmanager mgr = Client.getconnectionmanager ();
Httpparams params = Client.getparams ();
Client = new Defaulthttpclient (new Threadsafeclientconnmanager (params, mgr.getschemeregistry ()), params);
return client; } public void Postnoresult (final context, final string url, final map<string, string> maps, final string
Show) {new Thread () {@Override the public void run () {try {HttpPost post = new HttpPost (URL);
list<namevaluepair> params = new arraylist<namevaluepair> ();
For (String Key:maps.keySet ()) {Params.add (New Basicnamevaluepair (Key, Maps.get (key)); } post.setentity (New urlencodedformentity (params, HTTP.
Utf_8)); HttpResponse response = Httpclient.execute (post);//Error location if (reSponse.getstatusline (). Getstatuscode () = Looper.prepare ();
String r = entityutils.tostring (Response.getentity ());
Toastutil.print_log (R);
if (show!= null) {toastutil.show;
} looper.loop ();}
catch (Unsupportedencodingexception e) {e.printstacktrace ();
catch (Clientprotocolexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
}}}.start ();
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.