在ICS上,我在使用UrlConnection时遇到android.os.networkonmainthreadException错误 – 即使我在一个运行在它自己的进程上的服务中发出此请求,并且被异步调用以通过Messenger完成.
更改StrictPolicy无效,我仍然收到错误.
我能做什么?
编辑:此服务在一个单独的进程中运行 – 具有不同的pID和所有内容.
最佳答案根据documentation,服务仍在主线程上运行:Note that services,like other application objects,run in the main thread of their hosting process. This means that,if your service is going to do any cpu intensive (such as MP3 playback) or blocking (such as networking) operations,it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
因此,对于长时间运行的任务(例如使用UrlConnection),您应该使用类似IntentService的东西或自己生成新的线程. 总结
以上是内存溢出为你收集整理的服务器中的android.os.NetworkOnMainThreadException在一个单独的进程中全部内容,希望文章能够帮你解决服务器中的android.os.NetworkOnMainThreadException在一个单独的进程中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)