对于NetworkOnMainThreadException快速修复 [英] Quick fix for NetworkOnMainThreadException

查看:172
本文介绍了对于NetworkOnMainThreadException快速修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行第三方的开源计划,该计划抛出NetworkOnMainThreadException。根据参考SDK,这只是抛出靶向蜂窝SDK或更高的应用程序。针对早期SDK版本的应用程序可以做的网络在他们的主事件循环线程。

I need to execute third-party open source program, which throws NetworkOnMainThreadException. According to SDK reference, this is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads.

在第一个阶段,我只是想运行的程序,而无需更改源。所以,我从改了行AndroidManifesr.xml:

On the first stage I just want to run the program, without changing the source. So, I changed the line in AndroidManifesr.xml from:


    android:targetSdkVersion="15"


    android:targetSdkVersion="10"

不过,这并没有帮助,而且程序仍引发NetworkOnMainThreadException。我怎样才能让这个工作?我想执行在Android仿真谷歌的API(16级)的程序。

However, this doesn't help, and program still throws NetworkOnMainThreadException. How can I make this to work? I am trying to execute the program on Android Emulation Google APIs (level 16).

推荐答案

您可以将其更改为:

android:targetSdkVersion="9"

API 10对应于蜂窝,而9是姜饼。这种现象仅出现在API的10以上。

API 10 corresponds to honeycomb, while 9 is gingerbread. This behavior is only seen in APIs 10 and above.

不过,我会反对这项建议。相反,你应该将任何长期运行的操作或操作与运行长到后台线程,像一个的 AsyncTask的

However, I would advise against this. Instead, you should move any long running operations, or operations with the possibility of running for long into a background thread, like an AsyncTask.

您也可以尝试通过设置严格的模式关闭:

You could also try to set Strict Mode off using:

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

这篇关于对于NetworkOnMainThreadException快速修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆