NetworkOnMainThreadException When reading from web

穿精又带淫゛_ 提交于 2019-11-29 12:11:14

don't use network activities in main thread.This exception only thrown for applications targeting the Honeycomb SDK or higher. perform all the network related task in separate thread ( either use handler looper or runOnUiThread) . your problem will get solved.

Either you can move it to background thread or if you just want to eliminate this error just add this in your onCreate() :

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

    StrictMode.setThreadPolicy(policy); 

Do not forget to add internet permission in manifest:

<uses-permission android:name="android.permission.INTERNET"/>

You might want to do all your downloading using a AsyncTask, http://developer.android.com/reference/android/os/AsyncTask.html

or if your on api 11+ a AsyncTaskLoader, http://developer.android.com/reference/android/content/AsyncTaskLoader.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!