安卓httpclient.execute异常 [英] android httpclient.execute exception

查看:211
本文介绍了安卓httpclient.execute异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我测试的模拟器,它的工作原理,但在测试我的设备(银河S3),它不断抛出的错误,当在以下code:

 的Htt presponse响应;
        响应= httpclient.execute(httppost);
 

例外:

  8月9日至三号:16:49.018:E / AndroidRuntime(24254):java.lang.RuntimeException的:无法启动的活动ComponentInfo {sg.dianping / sg.dianping.activity。 ItemListActivity}:android.os.NetworkOnMainThreadException
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.app.ActivityThread.access $ 600(ActivityThread.java:128)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1161)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.os.Handler.dispatchMessage(Handler.java:99)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.os.Looper.loop(Looper.java:137)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.app.ActivityThread.main(ActivityThread.java:4517)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在java.lang.reflect.Method.invokeNative(本机方法)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在java.lang.reflect.Method.invoke(Method.java:511)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:993)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在dalvik.system.NativeStart.main(本机方法)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):由:android.os.NetworkOnMainThreadException
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
8月9日至3号:16:49.018:E / AndroidRuntime(24254):在java.net.InetAddress.lookupHostByName(InetAddress.java:391)
 

解决方案

这异常的时候,好了,你的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

这篇关于安卓httpclient.execute异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆