Caused by: android.os.NetworkOnMainThreadException

问题:

Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102) at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90) at java.net.InetAddress.getAllByName(InetAddress.java:787) at com.android.okhttp.Dns$1.lookup(Dns.java:39) at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175) at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141) at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83) at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174) at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126) at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244) at java.net.URL.openStream(URL.java:1058) at kotlin.io.TextStreamsKt.readBytes(ReadWrite.kt:144) at com.easy.kotlin.bean.MovieContent.<init>(MovieContent.kt:18) at com.easy.kotlin.bean.MovieContent.<clinit>(MovieContent.kt:10) at com.easy.kotlin.MovieListActivity.setupRecyclerView(MovieListActivity.kt:37) at com.easy.kotlin.MovieListActivity.onCreate(MovieListActivity.kt:33) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

主线程不能访问网络,在访问网络的代码前面添加如下代码即可:

StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);

或者新开子线程(建议这种方法):

new Thread(new Runnable() { @Override public void run() { Document doc = null; try { doc = Jsoup.connect("http://www.baidu.com").get();// Jsoup是我用的一个牛叉的jar包 } catch (IOException e) { e.printStackTrace(); } Log.d("doc", doc.toString()+""); } }).start();

There are two Solution of this Problem.

  1. Don't write network call in Main UI Thread, Use Async Task for that.
  2. Write below code into your MainActivity file after setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } And below import statement into your java file.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏技术小黑屋

Scan Media Files in Android

I once tried to use MediaScanner to resolve problems; however it turned out to b...

26420
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

16420
来自专栏流媒体

Activity启动流程源码分析

launcher就是android桌面应用程序。也是操作系统启动有第一个app。同时作为其他app的入口。我们找到其源码 android-6.0.0_r1\p...

23610
来自专栏一个会写诗的程序员的博客

$ gradle packageDebug --stacktrace error: top-left corner pixel must be either opaque white or trans

$ gradle packageDebug --stacktrace error: top-left corner pixel must be either ...

12920
来自专栏Linyb极客之路

你该怎么做?当你发现springboot扫描controller没有生效

加上@SpringBootApplication为啥就能扫描到,其实我也不知道。那就看下这个注解到底是个啥样的奇葩,它的注解构造了解一下

3.9K30
来自专栏Pulsar-V

OpenCV编译参数一览

全部参数一览 //Path to a program. ANT_EXECUTABLE:FILEPATH=D:/apache-ant-1.10.1/bin/ant...

86560
来自专栏智能计算时代

docs and demos of Watson services

Services 1)Language 1.1) AlchemyLanguage :Demo Document AlchemyLanguage is a c...

27660
来自专栏西安-晁州

golang学习之beego增删改查代码实现

记录下使用beego的增删改查实现,数据库使用mysql,完整代码如下: package main import ( _ "crud_beego/ro...

24400
来自专栏24k

Spark Standalone Mode 单机启动Spark -- 分布式计算系统spark学习(一)

24350
来自专栏CreateAMind

代码:Learning by Playing –Solving Sparse Reward Tasks from Scratch

19310

扫码关注云+社区

领取腾讯云代金券