Android开发之NetworkOnMainThreadException异常

        话说刚开发的一个小应用,在Android2.3上跑的刚刚的,换个4.0的系统后,傻眼了,直接程序Crash。adb logcat看了下,一串常常的信息,如下:

E/AndroidRuntime( 3454): FATAL EXCEPTION: main
E/AndroidRuntime( 3454): android.os.NetworkOnMainThreadException
E/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


提示显示是由于进行httpget()操作时,引发的该异常,异常名字显示是主线程中的网络异常。为啥会这样呢,求助了下Google大神,得神人相助,才知Android4.0系统严格限制在UI主线程中进行耗时操作,如这里的网络IO,否则就抛该异常,整死你。

解决办法:利用多线程或者后台服务来完成耗时操作即可


参考博文:

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




你可能感兴趣的:(Android开发之NetworkOnMainThreadException异常)