nMainThreadException即使使用的AsyncTa

nMainThreadException即使使用的AsyncTa

本文介绍了Android的蜂窝:NetworkOnMainThreadException即使使用的AsyncTask,并没有严格的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰上了NetworkOnMainThreadException我的Andr​​oid 3.0应用程序。寻找一个解决方案,我发现这个的,但如果我正确地理解这一点,默认设置将是严格的模式被关闭。

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.

所以,我很绝望,现在我应该做的,以prevent这...

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:此线程禁止HTTP请求,却发现了一个解决方案的。我有点糊涂了,不过,因为 AndroidHttpClient 工作正常,当我在我的Andr​​oid用它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是一个不错的想法,但pretty的无用的,如果done错 ...所以,没有什么不对的严格模式下的反应。应该听,呃? ;)

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? ;)

还是不错的知道它是默认情况下在蜂窝激活。

Still good to know it's activated on Honeycomb by default.

推荐答案

StrictMode默认情况下,在蜂窝开启。

StrictMode is turned on by default in Honeycomb.

请参阅说链接具体 penaltyDeathOnNetwork()。我遇到了类似的问题。

See say link specifically penaltyDeathOnNetwork(). I ran into a similar problem.

这篇关于Android的蜂窝:NetworkOnMainThreadException即使使用的AsyncTask,并没有严格的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:03