Android.os.NetworkOnMainThreadException exception

Source: Internet
Author: User

Ext.: http://blog.csdn.net/wotoumingzxy/article/details/7797295

This exception probably means an exception when the main thread accesses the network. Android's version prior to 4.0 supported access to the network in the main thread, but after 4.0 it was optimized for this part of the program, meaning that the code to access the network could not be written in the main thread.

So what do you do to get it working? Please see here:
For example, get a URL address from the network.

    url = Authutil.getauthorizationurl ();      if (url = = null) {          toast.maketext (webviewactivity.this, R.string.auth_url_empty,.). Show ();        

If it is written directly in the main thread, it will still work in the 2.x version, but after 4.0, an error occurs.

There are a lot of relevant information on the Internet, such as the main thread to add

    See Strictmode document      Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()              . Detectdiskreads ()              . Detectdiskwrites ()              . Detectnetwork ()   //or. Detectall () for all detectable problems              . Penaltylog ()              . Build ());      Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()              . Detectleakedsqlliteobjects ()              . Detectleakedclosableobjects ()              . Penaltylog ().              Penaltydeath ().              build ());  

But

. Detectleakedclosableobjects () will error,

This means that this method is not used, but the view source found its method is public

   /* * * Detect when an                  {@link java.io.Closeable} or other                  * object with a explict termination method is finalized
   * without had been closed.                  *                  * <p>you always want to explicitly close such objects to                  * Avoid unnecessary resources leaks.                  */Public                  Builder detectleakedclosableobjects () {                      return enable (Detect_vm_closable_leaks);                  }  

I'm a little confused about this, but the seniority is not yet. Ha ha.

So I just load the data in the second way, multi-threaded, asynchronous loading.

The code is as follows:

Send an empty message in the main function:

    New Thread () {                  @Override public                  void Run () {                      //TODO auto-generated method Stub                      super.run ();                      url = Authutil.getauthorizationurl ();                      if (url = = null) {                          toast.maketext (webviewactivity.this, R.string.auth_url_empty,.). Show ()                      ;                      Handler.sendemptymessage (0);                  }              }. Start ();  

The message is received in handler and processed accordingly.

Private Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {case 0:<span style= "White-space:pre" ></span>load (URL, webView); break;};};

this resolves the exception.

If there is anything wrong, please give me more advice.

October 16, 2014 14:15:58

In essence, in the current development of Android, the part about network access can not be put into the main thread!

Android.os.NetworkOnMainThreadException Exception (RPM)

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.