Android. OS. networkonmainthreadexception

Source: Internet
Author: User
ArticleDirectory
    • // Start the thread to execute the download task

Recent testsProgramIf the test is normal on the mobile phone end, the exception Android. OS. networkonmainthreadexception will always be thrown during Internet connection.

That is to say, you cannot perform online operations in the main thread.

In 4.0, the access network cannot be carried out in the main program. There are two solutions: one is to add in the main program:

Strictmode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

// For details, see the 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 ());

 

 

The other is to start the thread to execute the download task:

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

// Start the thread to execute the download task

New thread (downloadrun). Start ();

}

/**

* Download thread

*/

Runnable downloadrun = new runnable (){

@ Override

Public void run (){

// Todo auto-generated method stub

Updatelistview ();

}

};

Related Article

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.