Android. OS. networkonmainthreadexception (Android version is really disgusting)

Source: Internet
Author: User

Really angry, the Code previously written is re-imported today, causing a lot of problems,

At first, an error was reported. After half a day, it was a version problem. Then Baidu solved the problem.

Http://blog.csdn.net/wang6279026/article/details/8564248

Later, I said that android. OS. networkonmainthreadexception is abnormal. I thought there was a problem with the code I wrote earlier. (I am not very good at network operations ). So I have been searching for code errors. I couldn't find the result, and Baidu found that the version was still a problem. I upgraded all day and I was speechless. A lot of time is wasted.

The upgraded version cannot use HTTP requests in the main thread; otherwise, an error is returned. Maybe the designer has discovered that blocking in the main thread may cause serious consequences. There are two solutions:

One is the well-known handler.

One is to add the following code in the oncreate method:

Strictmode. setthreadpolicy (New strictmode. threadpolicy. builder (). detectdiskreads (). detectdiskwrites (). detectnetwork (). penaltylog (). build (); strictmode. setvmpolicy (New strictmode. vmpolicy. builder (). detectleakedsqlliteobjects (). detectleakedsqlliteobjects (). penaltylog (). penaltydeath (). build ());

Then we can. By the way, do not forget to add network permissions.

Related Article

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.