Android. OS. networkonmainthreadexception Exception Handling

Source: Internet
Author: User

The android. OS. networkonmainthreadexception is reported when the mobile phone number is Android 2.1.

The error is caused by a problem with the WebService accessing the network.

I found it online for a long time and said it was used.

@ Override
Protected void oncreate (bundle savedinstancestate)
{
// Todo auto-generated method stub
String strver = getversion. getsystemversion ();
Strver = strver. substring (0, 3). Trim ();
Float FV = float. valueof (strver );
If (FV> 2.3)
{
Strictmode. setthreadpolicy (New strictmode. threadpolicy. Builder ()
. Detectdiskreads ()
. Detectdiskwrites ()
. Detectnetwork () // replace it with detectall (), which includes disk read/write and network I/O.
. Penaltylog () // print logcat. You can also locate Dropbox and save the corresponding log through the file.
. Build ());
Strictmode. setvmpolicy (New strictmode. vmpolicy. Builder ()
. Detectleakedsqlliteobjects () // detects SQLite database operations
. Penaltylog () // print logcat
. Penaltydeath ()
. Build ());
}
Super. oncreate (savedinstancestate );

}

CodeNote that the strictmode version is 2.3 or later. I can't upgrade the project to 2.3 again.

In this case, an error is reported after the installation is completed in versions earlier than 2.3, And the next version is determined before strictmode is called.

A new strictmode class is provided from Android 2.3, which can be used to captureProgramTime-consuming disk, network access, or function calls in the main thread can help developers improve programs and make the main thread process UIS and animations smoother during disk read/write and network operations, prevent the main thread from being blocked.

 

Ten Suggestions for Android app developers

Android. OS. Build. version. Release; returns the current system version.

The above problems can be solved.

 

Note: The cause of this error is that the Code does not comply with the android standard. If you change the preceding access method to an asynchronous operation, the android. OS. networkonmainthreadexception exception will not occur during access on 4.0.

For example:

New thread (){
@ Override
Public void run (){
// The method you want to execute
// Send an empty message to handler after execution.
Handler. sendemptymessage (0 );
}
}. Start ();

 

// define the handler object
private handler = new handler () {
@ override
// execute the handler method when a message is sent.
Public void handlemessage (Message MSG) {
super. handlemessage (MSG);
// processing UI
}< BR >};

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.