android.os.NetworkOnMainThreadException異常如何解決

來源:互聯網
上載者:User
android.os.NetworkOnMainThreadException08-08 17:53:30.635 I/ArticleTable(22461): 添加成功 5808-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" />


這樣就可以了!


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.