1

I have an android project that is set to run on android 3.2, and I have a library project that uses networking in its main thread, which by sdk version of 3,2 throws android.os.NetworkOnMainThreadException. By SDK version below Honeycomb using network in main thread is ok: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html.

To solve this problem, I tried setting my library project's SDK version to 1.6, however the same error is still there! Is this normal behavior of Android? Isn't it wrong? What about backward compatibility?

Edit: I know that I should not perform a network request in main thread, however its not my code that does it, but the library that I have to use. I cannot/would not like to touch the code of the library, its a commercial API that belongs to other company.

Edit 2: I am of course trying to get a newer/fixed version of that library, but that is another issue. So my question now is, is there a way to compile/run the library with an older SDK?

4

2 回答 2

2

我认为底线是,如果您尝试在 3.0+ 设备/模拟器上运行它,它将无法工作。您需要使用 AsyncTask。

于 2012-08-15T15:00:15.660 回答
1

您正在使用的库项目不应在主线程上执行网络操作。Android 抛出这个异常是因为这样做是非常糟糕的做法。

你不应该破解你的方式NetworkOnMainThreadException; 使用 anAsyncTask或 aThread来执行网络操作。

阅读这篇博文了解更多信息。

于 2012-08-15T15:02:53.600 回答