且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

安卓httpclient.execute异常

更新时间:2023-11-19 14:14:52

这异常的时候,好了,你的UI线程上执行网络活动 NetworkingOnMainThread 被抛出。它有阻塞主线程的潜力,因为它等待,直到网络连接恢复,从而阻塞线程之前完成。

您有2个选项。

1 - 将所有的网络到一个不同的线程。最常见和最简单的办法是 AsyncTask的 但也有其他的选择,比如处理程序。这是建议的选项。

2 - 你可以改变政策,让网络在UI线程上。

只是添加此code

  StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建立()。;
StrictMode.setThreadPolicy(政策);
 

这个选项是不推荐,因为它只是绕过了异常。唯一的例外是抛出的一个原因。我会选择第一个选项。有关于如何实现许多教程的AsyncTask

好运气

When I test on simulator, it works, but when test with my device (galaxy s3), it keeps throwing errors, when during the following code:

HttpResponse response;
        response = httpclient.execute(httppost);

the exceptions:

09-03 08:16:49.018: E/AndroidRuntime(24254): java.lang.RuntimeException: Unable to start activity ComponentInfo{sg.dianping/sg.dianping.activity.ItemListActivity}: android.os.NetworkOnMainThreadException
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.os.Looper.loop(Looper.java:137)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.app.ActivityThread.main(ActivityThread.java:4517)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at java.lang.reflect.Method.invokeNative(Native Method)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at java.lang.reflect.Method.invoke(Method.java:511)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at dalvik.system.NativeStart.main(Native Method)
09-03 08:16:49.018: E/AndroidRuntime(24254): Caused by: android.os.NetworkOnMainThreadException
09-03 08:16:49.018: E/AndroidRuntime(24254):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
09-03 08:16:49.018: E/AndroidRuntime(24254):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)

This exception NetworkingOnMainThread is thrown when, well, you perform network activity on the UI thread. It has potential of blocking the main thread, since it waits until the network connection is finished before resuming, thus blocking the thread.

You have 2 options.

1 - Move all your networking to a different thread. Most common and easy solution is AsyncTask But there are other options, such as Handlers. This is the recommended option.

2- You could change the policy to allow networking on the UI thread.

just add this code

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

This option is not recommended since it just bypasses the exception. The exception is thrown for a reason. I'd choose the first option. There are many tutorial on how to implement AsyncTask.

Good luck