android.os.NetworkOnMainThreadException

1,277 views
Skip to first unread message

salles pro

unread,
May 14, 2013, 7:43:02 PM5/14/13
to faye-...@googlegroups.com
After a couple of minutes running, the service crashes because of the network access on the main thread.

Android 4.0.4 running on motorola RAZR max


05-14 20:30:45.748: E/AndroidRuntime(18430): FATAL EXCEPTION: main

05-14 20:30:45.748: E/AndroidRuntime(18430): android.os.NetworkOnMainThreadException

05-14 20:30:45.748: E/AndroidRuntime(18430): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)

05-14 20:30:45.748: E/AndroidRuntime(18430): at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:168)

05-14 20:30:45.748: E/AndroidRuntime(18430): at libcore.io.IoBridge.sendto(IoBridge.java:487)

05-14 20:30:45.748: E/AndroidRuntime(18430): at java.nio.SocketChannelImpl.writeImpl(SocketChannelImpl.java:378)

05-14 20:30:45.748: E/AndroidRuntime(18430): at java.nio.SocketChannelImpl.write(SocketChannelImpl.java:336)

05-14 20:30:45.748: E/AndroidRuntime(18430): at com.sallespro.mobile.WebSocket.send(WebSocket.java:315)

05-14 20:30:45.748: E/AndroidRuntime(18430): at com.sallespro.mobile.FayeClient.handShake(FayeClient.java:139)

05-14 20:30:45.748: E/AndroidRuntime(18430): at com.sallespro.mobile.FayeClient.access$1(FayeClient.java:137)

05-14 20:30:45.748: E/AndroidRuntime(18430): at com.sallespro.mobile.FayeClient$1.handleMessage(FayeClient.java:98)

05-14 20:30:45.748: E/AndroidRuntime(18430): at android.os.Handler.dispatchMessage(Handler.java:99)

05-14 20:30:45.748: E/AndroidRuntime(18430): at android.os.Looper.loop(Looper.java:137)

05-14 20:30:45.748: E/AndroidRuntime(18430): at android.app.ActivityThread.main(ActivityThread.java:5118)

05-14 20:30:45.748: E/AndroidRuntime(18430): at java.lang.reflect.Method.invokeNative(Native Method)

05-14 20:30:45.748: E/AndroidRuntime(18430): at java.lang.reflect.Method.invoke(Method.java:511)

05-14 20:30:45.748: E/AndroidRuntime(18430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)

rieu dung

unread,
Jun 13, 2013, 7:35:36 PM6/13/13
to faye-...@googlegroups.com
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

            StrictMode.setThreadPolicy(policy);
please add these line after setContentView(R.layout.someThing);

salles

unread,
Jun 14, 2013, 11:22:15 AM6/14/13
to faye-...@googlegroups.com
Hi, this is a shortcut and will not really work, depending on the way you implement the connection.

i recommend using either of the two implementations bellow which are best fit to not blocking the Main Thread.

Reply all
Reply to author
Forward
0 new messages