Networkonmainthreadexception in Android Development

Source: Internet
Author: User

It is said that a newly developed small application is just running on android2.3. After switching to a 4.0 system, the program crash is dumpfounded. ADB logcat reads a string of common information, as follows:

E/AndroidRuntime( 3454): FATAL EXCEPTION: mainE/AndroidRuntime( 3454): android.os.NetworkOnMainThreadExceptionE/AndroidRuntime( 3454): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)E/AndroidRuntime( 3454): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)E/AndroidRuntime( 3454): at libcore.io.IoBridge.recvfrom(IoBridge.java:503)E/AndroidRuntime( 3454): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)E/AndroidRuntime( 3454): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)E/AndroidRuntime( 3454): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)E/AndroidRuntime( 3454): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)E/AndroidRuntime( 3454): at java.io.BufferedInputStream.read(BufferedInputStream.java:227)E/AndroidRuntime( 3454): at libcore.net.http.HttpConnection.isStale(HttpConnection.java:256)E/AndroidRuntime( 3454): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:71)E/AndroidRuntime( 3454): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)E/AndroidRuntime( 3454): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)E/AndroidRuntime( 3454): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)E/AndroidRuntime( 3454): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)E/AndroidRuntime( 3454): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)E/AndroidRuntime( 3454): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)E/AndroidRuntime( 3454): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)E/AndroidRuntime( 3454): at com.raycommtech.ipcam.Util.httpGet(Util.java:60)E/AndroidRuntime( 3454): at com.raycommtech.ipcam.Util.getVideoInfoByDDNS(Util.java:209)E/AndroidRuntime( 3454): at com.raycommtech.ipcam.VideoInfo.refresh(VideoInfo.java:49)E/AndroidRuntime( 3454): at com.raycommtech.ipcam.imp.p2p.MediaFetchRayCommP2P.<init>(MediaFetchRayCommP2P.java:54)E/AndroidRuntime( 3454): at com.raycommtech.ipcam.MediaFetchFactory.makeMeidaFetch(MediaFetchFactory.java:33)E/AndroidRuntime( 3454): at com.raycommtech.rayfastcfg.service.RayFastcfgService.getMediaFetch(RayFastcfgService.java:88)E/AndroidRuntime( 3454): at com.raycommtech.rayfastcfg.wlan.WifiConfigActivity$1.onServiceConnected(WifiConfigActivity.java:132)E/AndroidRuntime( 3454): at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1068)E/AndroidRuntime( 3454): at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1085)E/AndroidRuntime( 3454): at android.os.Handler.handleCallback(Handler.java:605)E/AndroidRuntime( 3454): at android.os.Handler.dispatchMessage(Handler.java:92)E/AndroidRuntime( 3454): at android.os.Looper.loop(Looper.java:137)E/AndroidRuntime( 3454): at android.app.ActivityThread.main(ActivityThread.java:4444)E/AndroidRuntime( 3454): at java.lang.reflect.Method.invokeNative(Native Method)E/AndroidRuntime( 3454): at java.lang.reflect.Method.invoke(Method.java:511)E/AndroidRuntime( 3454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)E/AndroidRuntime( 3454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)E/AndroidRuntime( 3454): at dalvik.system.NativeStart.main(Native Method)E/hubo_EventLog(  309): OnException

The prompt is displayed because the exception is thrown during the httpget () operation, and the Exception name is displayed as a network exception in the main thread. Why is that? I have asked for help from the Google gods. Only with the help of others can I know that the android4.0 system strictly limits the time-consuming operations in the UI main thread, such as network I/O, otherwise, you will be killed.

Solution: use multiple threads or backend services to perform time-consuming operations.

Refer to blog:

Http://www.cnblogs.com/freexiaoyu/archive/2012/04/13/2445707.html

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.