Android.os.NetworkOnMainThreadException Exception Handling

Source: Internet
Author: User

Item is 2.1 Dev Test phone is android4.0 when prompt android.os.NetworkOnMainThreadException exception

Out of the wrong place is the webservice of accessing the network has a problem

On the internet for a long day to say it is 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 ()//This can be replaced with Detectall () which includes disk read-write and network I/O
. Penaltylog ()//print Logcat, of course, you can also navigate to Dropbox and save the corresponding log by file
. build ());
Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()
. Detectleakedsqlliteobjects ()//probing SQLite database operations
. Penaltylog ()//Print Logcat
. Penaltydeath ()
. build ());
}
Super.oncreate (savedinstancestate);

}

Code note Strictmode is 2.3 or more, and I've upgraded the project to 2.3.

But do this 2.3 after the installation of the following version of the error after opening, before calling Strictmode to make the next version of the judgment

Starting with Android 2.3, a new class, Strictmode, can be used to capture time-consuming disk, network access, or function calls that occur in the main thread of the application, helping developers to improve their programs, making the main thread processing UI and animations smoother when disk reads and writes and network operations. Prevent the main thread from being blocked.

10 tips 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 conform to the Android specification, if the above access mode is changed to an asynchronous operation will not appear on the 4.0 access to the android.os.NetworkOnMainThreadException exception

Such as:

New Thread () {
@Override
public void Run () {
The method you want to execute
Send an empty message to handler after execution is complete
Handler.sendemptymessage (0);
}
}.start ();

Defining Handler Objects
Private Handler Handler =new Handler () {
@Override
This is the way to execute handler when a message is sent out.
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
Working with the UI
}
};

You can consider using volley http://www.cnblogs.com/freexiaoyu/p/3446028.html

On Google I/O 2013, Volley was released. Volley is a network communication library on the Android platform that makes network communication faster, simpler, and more robust.

Android.os.NetworkOnMainThreadException Exception Handling

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.