Quick fix for NetworkOnMainThreadException(NetworkOnMainThreadException 的快速修复)
问题描述
我需要执行第三方开源程序,抛出NetworkOnMainThreadException.根据 SDK 参考,这仅适用于针对 Honeycomb SDK 或更高版本的应用程序.允许针对早期 SDK 版本的应用程序在其主事件循环线程上进行联网.
在第一阶段,我只想运行程序,而不更改源.因此,我将 AndroidManifesr.xml 中的行从:
<上一页>安卓:targetSdkVersion="15"到:
<上一页>安卓:targetSdkVersion="10"但是,这并没有帮助,程序仍然抛出 NetworkOnMainThreadException.我怎样才能使它工作?我正在尝试在 Android Emulation Google API(16 级)上执行该程序.
你可以改成:
android:targetSdkVersion="9"
API 10 对应蜂窝,而 9 是姜饼.此行为仅在 API 10 及更高版本中可见.
但是,我建议不要这样做.相反,您应该将任何长时间运行的操作或可能长时间运行的操作移到后台线程中,例如 AsyncTask.
您也可以尝试使用以下方法关闭严格模式:
StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(policy);
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.
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"
to:
android:targetSdkVersion="10"
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).
You could change it to:
android:targetSdkVersion="9"
API 10 corresponds to honeycomb, while 9 is gingerbread. This behavior is only seen in APIs 10 and above.
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 的快速修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NetworkOnMainThreadException 的快速修复
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01