Android Program Connection Network appears android.os.NetworkOnMainThreadEx

Source: Internet
Author: User

Android Program Connection Network appears android.os.NetworkOnMainThreadEx

Error:

Android.os.NetworkOnMainThreadException

At Android.os.strictmode$androidblockguardpolicy.onnetwork (strictmode.java:1099) ...

Cause: The access network cannot run in the main program when it comes to URL operations

Solution: Refer to "http://geeksun.iteye.com/blog/1447708, http://kb.cnblogs.com/a/2311659/"

Add the following code to the OnCreate method of the activity class, and the thread's method has not yet been tested.

    public void OnCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (r.layout.main);                Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder (). Detectdiskreads ().        detectdiskwrites (). Detectnetwork (). Penaltylog (). build ());        Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects ()        . Detectleakedclosableobjects (). Penaltylog (). Penaltydeath (). build ());    }

The official Android document gives the purpose of this class setting: Strictmode is a system-provided development tool that detects potential problems with the system due to accidental accidents during development, prompting the developer to fix them.

Strictmode is typically used to capture problems with disk access or interaction with the main process in network access, because in the main process, UI actions and some actions are most frequently used, and there are conflicting issues between them. Stripping disk access and network access from the main thread can make disk or network access smoother, increasing responsiveness and user experience.

Obviously, the majority of beginners in the network development, will choose to access the network code directly into the main process, due to the main process of the primary work--ui interaction--contradiction, therefore, must set up a certain detection mechanism to ensure the smooth operation of the system, all the anomalies can be detected.

public static voidsetthreadpolicy (Strictmode.threadpolicy policy) This method allows us to set a set of thread-running policy mechanisms for the current application. The parameter is a policy group (that is, a set of policies). public static Finalclass StrictMode.ThreadPolicy.Builder () Builder is an inline class for the inline class Threadpolicy in Strictmode. Here we call its default constructor method. Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). Build ()

In this way, we set up a set of monitoring mode, we want to detect the disk read and write, network access, log violations and so on.

The second statement sets up a set of monitoring policies for the virtual machine, with the same parameters, and therefore no longer repeat them. In this way, the main thread allows us to access network resources after the network and disk access are guaranteed to be controlled.

Android Program Connection Network appears android.os.NetworkOnMainThreadEx

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.