gpt4 book ai didi

java - Android 中的套接字连接错误

转载 作者:行者123 更新时间:2023-12-01 11:46:18 25 4
gpt4 key购买 nike

我在 android 中连接 tcp 时遇到问题。异常(exception)是这样的:

03-17 17:39:29.374    1089-1089/com.example.dhorka.client E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.dhorka.client, PID: 1089
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
at libcore.io.IoBridge.connect(IoBridge.java:122)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
at java.net.Socket.startupSocket(Socket.java:590)
at java.net.Socket.<init>(Socket.java:226)
at com.example.albert.client.ConnectionManagement.run(ConnectionManagement.java:39)
at java.lang.Thread.run(Thread.java:818)
at com.example.albert.client.MainActivity.click(MainActivity.java:43)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-17 17:39:31.479 1089-1089/com.example.dhorka.client I/Process﹕ Sending signal. PID: 1089 SIG: 9

不知道是什么问题。我使用的代码是:

public class ConnectionManagement implements Runnable  {
private static ConnectionManagement instance = new ConnectionManagement();
private String ip;
private int port;
private Socket connection;
private PrintWriter printWriter;

private ConnectionManagement(){
this.ip = "127.0.0.1";
this.port = 700;
}

public static ConnectionManagement getInstance(){
return instance;
}

public void setIp(String ip){
this.ip=ip;
}

public void setPort(int port){
this.port=port;
}

public void run(){
try {
connection = new Socket(InetAddress.getByName(this.ip), this.port);
PrintWriter printWriter = new PrintWriter(connection.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}

}
public void sendMessage(String message){
printWriter.write(message);
}

public void disconnect(){
try {

printWriter.flush();
printWriter.close();
connection.close();

}catch (IOException e){
e.printStackTrace();
}
}

public boolean isClosed(){
return connection.isClosed();
}

当我在主要 Activity 中调用此类时,我正在执行以下操作:

 case R.id.connect:
EditText editText = (EditText) findViewById(R.id.ip);
String ip = editText.getText().toString();
connection.setIp(ip);
new Thread(connection).run();
break;

我不知道我做错了什么。

感谢您的关注。

最佳答案

您尝试以错误的方式启动线程。如果你想启动新线程,你应该使用 start() 方法。现在,您只需在主线程中调用 run() 方法,就会出现 NetworkOnMainThreadException

更改:

new Thread(connection).run();

new Thread(connection).start();

R.id.connect情况下。

关于java - Android 中的套接字连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29105190/

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