android httpclient.execute exception

John picture John · Sep 3, 2012 · Viewed 8.3k times · Source

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)

Answer

La bla bla picture La bla bla · Sep 3, 2012

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