Quick fix for NetworkOnMainThreadException

前端 未结 3 1542
轻奢々
轻奢々 2020-12-03 22:01

I need to execute third-party open source program, which throws NetworkOnMainThreadException. According to SDK reference, this is only thrown for applications targeting the

3条回答
  •  旧巷少年郎
    2020-12-03 22:50

    You are performing network task on your UI Thread. The best way to perform network operation is to use AsyncTask or simply create another thread. However, if you want a quick and dirty way to get rid of the error; you can also use this

    ThreadPolicy tp = ThreadPolicy.LAX;
    StrictMode.setThreadPolicy(tp);
    

提交回复
热议问题