"Android Notes" cannot be network-operated in the main thread

Source: Internet
Author: User
Tags sqlite database

Seen on the Internet, I have also been encountered before:

1 04-26 14:36:14.663:e/androidruntime (10368): android.os.NetworkOnMainThreadException2 04-26 14:36:14.663:e/androidruntime (10368): at         android.os.strictmode$androidblockguardpolicy.onnetwork ( strictmode.java:1117)3 but I configured <uses-permission android:name= in the manifest. Android.permission.INTERNET "/>

The cause of this error is that the code does not conform to the Android specification, meaning that the network operation is abnormal in the main thread.

The general time-consuming operation is not allowed in the main line thread direct operation, such as network operation, etc.

Generally need to open a sub-thread to access the network, and then through the handler to update the interface.

Of course, it is also possible to do this test operation, refer to add the following code:

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3Strictmode.setthreadpolicy (NewStrictMode.ThreadPolicy.Builder ()4 . Detectdiskreads ()5 . Detectdiskwrites ()6. Detectnetwork ()//or. Detectall () for all detectable problems7 . Penaltylog ()8 . Build ()); 9Strictmode.setvmpolicy (NewStrictMode.VmPolicy.Builder ()Ten. Detectleakedsqlliteobjects ()//probing SQLite database operations One. Penaltylog ()//Print Logcat A . Penaltydeath () - . Build ());  -         Super. OnCreate (savedinstancestate); theSetcontentview (R.layout.activity_main);

Reference Original: http://www.apkbus.com/android-114524-1-1.html

"Android Notes" cannot be network-operated in the main thread

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.