gpt4 book ai didi

java - Android 与 websocket 通信(Node Websocket 服务器和 Android Java 客户端)。不适用于 Android 4

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:45 27 4
gpt4 key购买 nike

我尝试将 Websocket 用作 Android 客户端 - 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、android)与服务器(nodejs、websocket)通信吗?

谢谢。

最佳答案

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

通过 I/O 操作(网络访问最终是 I/O 操作)阻塞主线程将导致(在最坏的情况下)用户界面缓慢或 Android 杀死您的应用程序,因为它没有及时使用react。查看designing for responsiveness文档。

关于java - Android 与 websocket 通信(Node Websocket 服务器和 Android Java 客户端)。不适用于 Android 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com