android.os.NetworkOnMainThreadException异常如何解决

android.os.NetworkOnMainThreadException
08-08 17:53:30.635 I/ArticleTable(22461): 添加成功 58
08-08 17:53:30.648 W/System.err(22461): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-08 17:53:30.648 W/System.err(22461): 	at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
08-08 17:53:30.649 W/System.err(22461): 	at libcore.io.IoBridge.recvfrom(IoBridge.java:545)
08-08 17:53:30.649 W/System.err(22461): 	at java.net.PlainSocketImpl.read(PlainSocketImpl.java:489)
08-08 17:53:30.649 W/System.err(22461): 	at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
08-08 17:53:30.649 W/System.err(22461): 	at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
08-08 17:53:30.649 W/System.err(22461): 	at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
08-08 17:53:30.649 W/System.err(22461): 	at java.io.BufferedInputStream.read(BufferedInputStream.java:227)
08-08 17:53:30.649 W/System.err(22461): 	at libcore.net.http.HttpConnection.isStale(HttpConnection.java:256)
08-08 17:53:30.649 W/System.err(22461): 	at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:71)
08-08 17:53:30.649 W/System.err(22461): 	at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-08 17:53:30.650 W/System.err(22461): 	at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
08-08 17:53:30.650 W/System.err(22461): 	at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
08-08 17:53:30.650 W/System.err(22461): 	at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
08-08 17:53:30.650 W/System.err(22461): 	at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
08-08 17:53:30.650 W/System.err(22461): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:280)
08-08 17:53:30.650 W/System.err(22461): 	at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:174)
08-08 17:53:30.650 W/System.err(22461): 	at com.golfsec.util.DownloadUtil.downloadFile(DownloadUtil.java:101)
08-08 17:53:30.650 W/System.err(22461): 	at com.golfsec.handler.notice.ArticleHttpHandler$AttachHandler1.handleMessage(ArticleHttpHandler.java:543)
08-08 17:53:30.650 W/System.err(22461): 	at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 17:53:30.650 W/System.err(22461): 	at android.os.Looper.loop(Looper.java:154)
08-08 17:53:30.652 W/System.err(22461): 	at android.app.ActivityThread.main(ActivityThread.java:4624)
08-08 17:53:30.652 W/System.err(22461): 	at java.lang.reflect.Method.invokeNative(Native Method)
08-08 17:53:30.652 W/System.err(22461): 	at java.lang.reflect.Method.invoke(Method.java:511)
08-08 17:53:30.652 W/System.err(22461): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:965)
08-08 17:53:30.652 W/System.err(22461): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:732)
08-08 17:53:30.653 W/System.err(22461): 	at dalvik.system.NativeStart.main(Native Method)

在做android开发 大家也会经常莫名其妙遇到各种问题,今天遇到这个问题也很奇怪,项目一直采用的是android sdk2.2开发

上一个app没有出现如题的问题,但这个版本却出现了,后来在网上看有说添加代码

	         详见StrictMode文档
	        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
	                .detectDiskReads()
	                .detectDiskWrites()
	                .detectNetwork()   // or .detectAll() for all detectable problems
	                .penaltyLog()
	                .build());
	        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
	                .detectLeakedSqlLiteObjects()
	                .detectLeakedClosableObjects()
	                .penaltyLog()
	                .penaltyDeath()
	                .build());

这样也可以解决问题,但sdk就不能采用2.2了

还有种解决办法就是在AndroidManifest.xml 里面进行配置

最近之所以出现这样问题是我在里面这样写的

   <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="14" />

出现了问题

后来改为

   <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="8" />

这样就可以了!

上一篇:Android加载网络图片报android.os.NetworkOnMainThreadException异常


下一篇:Android Eclipseproject开发中的常见调试问题(二)android.os.NetworkOnMainThreadException 异常的解决的方法