Android.os.NetworkOnMainThreadException exception

Source: Internet
Author: User

Android 4.1 project: Share the Times with Sina Weibo:

Android.os.NetworkOnMainThreadException

Online search after know because of the version of the problem, after 4.0 in the main thread inside the HTTP request will be reported this error, perhaps is afraid of HTTP request time too long cause the program suspended animation situation it. Then on-line friends have also given the corresponding solution, which is called on the policy has countermeasures:

One: Add the following code to the OnCreate function inside the activity that initiated the HTTP request:

See Strictmode document        Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder (). Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ());        Strictmode.setvmpolicy (new StrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (). Detectleakedclosableobjects (). Penaltylog (). Penaltydeath (). build ());   

If you are doing a project that is not Android 4.0, you cannot see the Strictmode class. I also use the Internet to give the Com_weibo_android.jar. But when the jar is downloaded, it's 2.3, it's going to be converted to Android 4.0, and then the two lines of code are added to the OnCreate () function that shares the corresponding shareactivity. This will not be reported in this error.

Two: Use thread, Runnable, handler these three classes:

PublicvoidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);This. Setcontentview (R.layout.share_mblog_view);NewThread (runnable). Start ();} Handler Handler =NewHandler () {@OverridePublicvoidHandlemessage (Message msg) {Super. Handlemessage (msg); Bundle data =Msg.getdata (); String val = data.getstring ("value"); LOG.I ("MyLog", "request result--" + val);}} Runnable Runnable = new Runnable () {@Override public void Run () {////   todo:http reques T. //Message msg = new message (); Bundle data = new bundle (), data.putstring ("value", "request Result"), Msg.setdata (data), Handler.sendmessage (msg ); }}

Android.os.NetworkOnMainThreadException Exception

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.