Intereting Posts

Android与websocket通信(Node Websocket服务器和Android Java客户端)。 不要在Android 4上工作

我试图用作Android客户端Websocket – weberknecht用一个线程发送每一秒消息给一个Nodejs Websocket(js): Node WebSocket Server

它在Android 2或2.3.3上正常工作,但是当我在Android 4上我在websocket.connect()上得到一个错误;

你知道我为什么或者我做错了什么吗?

我的代码是:

public class MainThread extends Thread { private boolean is_running; private URI url = null; private de.roderick.weberknecht.WebSocket websocket = null; public MainThread() { this.is_running = false; try { url = new URI("ws://192.168.1.88:8088/"); websocket = new WebSocketConnection(url); websocket.setEventHandler(new WebSocketEventHandler() { public void onOpen(){ System.out.println("--open"); } public void onMessage(WebSocketMessage message){ System.out.println("--received message: " + message.getText()); } public void onClose(){ System.out.println("--close"); } }); } catch (WebSocketException wse) { wse.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (URISyntaxException use) { use.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } @Override public void run() { while(is_running){ try { websocket.send("hello world"); Thread.sleep(10000); } catch (WebSocketException wse) { wse.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } } public void set_running(boolean is_running) { if(is_running == true) try { websocket.connect(); this.is_running = is_running; } catch (WebSocketException e) { System.out.println(e.toString()); //e.printStackTrace(); } } } 

和我得到的Android 4的错误:

 ERROR/AndroidRuntime(562): FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.biskis.test.websocket/com.biskis.test.websocket.GameActivity}: android.os.NetworkOnMainThreadException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) at android.app.ActivityThread.access$600(ActivityThread.java:123) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4424) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) at libcore.io.IoBridge.connectErrno(IoBridge.java:127) at libcore.io.IoBridge.connect(IoBridge.java:112) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.Socket.startupSocket(Socket.java:566) at java.net.Socket.tryAllAddresses(Socket.java:127) at java.net.Socket.<init>(Socket.java:177) at java.net.Socket.<init>(Socket.java:149) at de.roderick.weberknecht.WebSocketConnection.createSocket(WebSocketConnection.java:238) at de.roderick.weberknecht.WebSocketConnection.connect(WebSocketConnection.java:83) at com.biskis.test.websocket.MainThread.set_running(MainThread.java:76) at com.biskis.test.websocket.GameActivity.onCreate(GameActivity.java:20) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) ... 11 more 

或者你知道如何实现这个更好的方式来通信客户端(Java,安卓)到服务器(nodejs,websocket)?

谢谢。

Solutions Collecting From Web of "Android与websocket通信(Node Websocket服务器和Android Java客户端)。 不要在Android 4上工作"

不要连接到主线程(即在GameActivity.onCreate )上的GameActivity.onCreate ,错误将消失。 您可以使用AsyncTask在后台连接到WebSocket。

通过I / O操作(和networking访问最终是一个I / O操作)阻塞主线程将导致(在最坏的情况下)呆滞的用户界面或Android杀死你的应用程序,因为它没有及时反应。 请参阅devise响应文档。