Strange NetworkOnMainThreadException in Android app?

a 夏天 提交于 2019-11-28 12:57:20

this line:

Caused by: android.os.NetworkOnMainThreadException

Tells you what is going on.

You are attempting to access a network function on the Main(UI) thread. starting with Honeycomb the system raises an Exception when you do this.

To fix you just need to move any thing that is touching the network to its own thread.

This is done to make sure you do not block the UI thread from handling any input events from the user.By blocking the UI thread your application cannot perform any event handling routines.

Normally most UI systems have a watchdog timer,which keeps watching for any long operation on the UI thread and if the UI thread is blocked for more than a threshold(probably 10-20 seconds in android devices varying by manufacturer/OS version) the watchdog interrupts and causes a 'Application Not responding'(a.k.a ANR) to pop-up.

If you just want an easy work-around this, specify a minimum SDK version of Froyo or Gingerbread and omit the targetSdkVersion:

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