Android Honeycomb:即使在使用 AsyncTask 且没有严格模式时,NetworkOnMainThreadException 也是如此? [英] Android Honeycomb: NetworkOnMainThreadException even when using AsyncTask and no strict mode?
问题描述
我的 Android 3.0 应用程序遇到了 NetworkOnMainThreadException.在寻找解决方案时,我发现了 this,但如果我理解正确,默认设置将是关闭严格模式.
I run into a NetworkOnMainThreadException with my Android 3.0 app. Searching for a solution I found this, but if I understand this correctly, default setting would be that the strict mode is turned off.
此外,我所有的网络访问都在 AsyncTask
中,所以无论如何我都看不到这个异常的意义.
Also, all my network access is in an AsyncTask
, so I don't see the point in this Exception anyway.
所以,我现在很绝望我应该做些什么来防止这种情况发生...
So, I'm quite desperate now what I should do to prevent this...
亲切的问候,水母
这篇博客文章说AsyncTask 应该足够了,但至少阐明了 StrictMode 点.
This blog entry says that AsyncTask should be enough, but at least clarifies the StrictMode point.
解决方案:
我关闭了 StrictMode(保留一些设置可能更好,但我不会被打扰...):
I turned off the StrictMode (its probably better to keep some settings but I couldn't be bothered...):
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
在那之后,我确实遇到了java.lang.RuntimeException:This thread forbids HTTP requests",但找到了解决方案此处.不过,我有点困惑,因为 AndroidHttpClient
在我的 Android 2.0+ 应用程序中使用它时运行良好......
After that, I did run into a "java.lang.RuntimeException: This thread forbids HTTP requests", but found a solution for this here. I'm a bit confused, though, as AndroidHttpClient
worked fine when I used it in my Android 2.0+ app...
解决方案,第 2 部分
事实证明,使用 AsyncTask 是个好主意,但如果 做错了...所以严格模式的反应没有任何问题.应该听过吧?;)
As it turned out, using AsyncTask was a nice idea but pretty useless if done wrong... So there was nothing wrong with the strict mode's reaction. Should have listened, er? ;)
知道它默认在 Honeycomb 上激活还是很高兴的.
Still good to know it's activated on Honeycomb by default.
推荐答案
StrictMode 在 Honeycomb 中默认开启.
StrictMode is turned on by default in Honeycomb.
具体参见 链接penaltyDeathOnNetwork()
.我遇到了类似的问题.
See say link specifically penaltyDeathOnNetwork()
. I ran into a similar problem.
这篇关于Android Honeycomb:即使在使用 AsyncTask 且没有严格模式时,NetworkOnMainThreadException 也是如此?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!