For android network connections, the problem of timeout and networkonmainthreadexception often occurs. The manual browser can access the web address normally.

Source: Internet
Author: User

When developing Android programs, whether you use urlconnection or httpclient operations, we often encounter timeout. If you check that the network is normal at this time, if you can access the target Web address through a manual browser, it is estimated that the following information is useful to you.

The problem I encountered was that the access URL on some 2.x mobile phones was normal, but an error was reported on the 4.x mobile phone:

03-07 13:17:06. 132: E/connectionmanager (25522): Android. OS. networkonmainthreadexception

Or some timeout exceptions

 

After reading the relevant information, we found that the system has added a class: strictmode since Android 2.3. This class changes the way the network is accessed.
Android official documentation provides the purpose of setting this class:
Strictmode is a development tool provided by the system. It is used to detect potential system problems caused by accidental accidents during the development process, and then prompts developers to fix them.
Strictmode is usually used to capture problems arising from interactions between disk access or network access and the main process, because in the main process, UI operations and the execution of some actions are most often used, conflicts may occur between them. Detaching disk access and network access from the main thread can make disk or network access smoother and improve response and user experience.
Obviously, most beginners choose to put the code that accesses the network directly into the main process during network development. Because it is in conflict with the primary task of the main process-UI interaction, A certain detection mechanism must be set up to ensure smooth system operation and detect all exceptions.
We recommend that you add these two commands in the android document:

 

234567891011 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()                     .detectDiskReads()                     .detectDiskWrites()                     .detectNetwork()   // or .detectAll() for all detectable problems                     .penaltyLog()                     .build());             StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()             .detectLeakedSqlLiteObjects() // Test SQLite database operations             .penaltyLog() // Print logcat             .penaltyDeath()             .build());

 

Even in this case, an error will still be reported on the phone of the 4. x system, but the program execution will not be interrupted;

 

 

In fact, my program is an Android app shell made by webview. js interacts with native through webview, allowing native to perform some proxy access operations, and HTML5 will perform storage display operations, that is to say, some of the storage operations and native network access performed by JS may be executed concurrently. strictmode is only better to detect.

 

 

This article is from changing the Internet | I want to change some of my traits and try my best. Please indicate the source and corresponding links when reprinting.

Permanent Link: http://www.changeself.com /? P = 342

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.