android中的套接字连接出错

时间:2015-03-17 16:58:41

标签: java android multithreading sockets

我在android中连接tcp时遇到问题。例外是:

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();
}

当我在主要活动中打电话给这个课时我会这样做:

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

我不知道自己做错了什么。

感谢您的关注。

1 个答案:

答案 0 :(得分:1)

您尝试以错误的方式启动线程。如果要启动新线程,则应使用start()方法。现在,您只需在主线程中调用run()方法,即可NetworkOnMainThreadException

变化:

new Thread(connection).run();

new Thread(connection).start();
R.id.connect案例中的