Android NetworkOnMainThreadException 的快速修复
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12650921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quick fix for NetworkOnMainThreadException
提问by Alex F
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.
我需要执行第三方开源程序,该程序抛出 NetworkOnMainThreadException。根据 SDK 参考,这仅针对针对 Honeycomb SDK 或更高版本的应用程序抛出。允许面向早期 SDK 版本的应用程序在其主事件循环线程上进行联网。
On the first stage I just want to run the program, without changing the source. So, I changed the line in AndroidManifesr.xml from:
在第一阶段,我只想运行程序,而不更改源。因此,我将 AndroidManifesr.xml 中的行从:
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).
但是,这无济于事,程序仍然抛出 NetworkOnMainThreadException。我怎样才能使这个工作?我正在尝试在 Android Emulation Google API(级别 16)上执行该程序。
回答by Raghav Sood
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.
API 10 对应蜂窝,而 9 是姜饼。此行为仅在 API 10 及更高版本中可见。
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.
但是,我建议不要这样做。相反,您应该将任何长时间运行的操作或可能长时间运行的操作移动到后台线程中,例如AsyncTask。
You could also try to set Strict Mode off using:
您还可以尝试使用以下方法关闭严格模式:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
回答by Infinity
You are performing network task on your UI Thread. The best way to perform network operation is to use AsyncTask or simply create another thread. However, if you want a quick and dirty way to get rid of the error; you can also use this
您正在 UI 线程上执行网络任务。执行网络操作的最佳方式是使用 AsyncTask 或简单地创建另一个线程。但是,如果您想要一种快速而肮脏的方法来摆脱错误;你也可以用这个
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
回答by EsmaeelQash
These are not solutions, you Just tell the tutelar to sleep and sill!!! the android decide not to let you make network operations in the main thread (UI thread), because of the "halts" that these operation can make (the ui stop respond). So, as I read, they tell you to not shoot your leg, and you find the way to do!!!
这些都不是解决方案,你只是告诉tutelar睡觉和sill!android 决定不让您在主线程(UI 线程)中进行网络操作,因为这些操作可以“停止”(ui 停止响应)。所以,正如我所读到的,他们告诉你不要射你的腿,你找到了方法!!!
the solution : separated thread, Or Async ...
解决方案:分离线程,或异步...