Android Exception Java.lang.IllegalStateException Solution _android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.