Android Socket致命错误

时间:2013-02-17 10:33:55

标签: java android sockets networking illegalstateexception

我的问题与此问题完全相同,但我还没有创建任何输出流

FATAL Exception Main android

    // Try to connect

    try {

        // Check the network state
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        // If there is conection, try to create a socket with remote server
        if (networkInfo.isConnected()) {
            InetAddress address = InetAddress
                    .getByName(serverAdressTextField.getText().toString());
            Socket serverConnection = new Socket(address, PORT);


            // Write message of success
            //TODO Implement the rest of login function
            Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        } else {

            // Say user that there is no internet

            Toast.makeText(this, "No connection", Toast.LENGTH_SHORT)
                    .show();
        }



    // Catch unknown host and prompt error
    } catch (UnknownHostException e) {

        Toast.makeText(this, "Server is not reached", Toast.LENGTH_SHORT)
                .show();
        return;

    // Prompt IO exception
    } catch (IOException e) {

        Toast.makeText(this, "I/O Error", Toast.LENGTH_SHORT).show();
        return;


    // Any other exception, not good
    } catch (Exception e) {

        Toast.makeText(this, "Unknown Error", Toast.LENGTH_SHORT).show();
        return;

    }

抛出IllegalStateException,如果没有捕获,则会抛出

    02-17 05:29:59.655: E/AndroidRuntime(18470): FATAL EXCEPTION: main
    02-17 05:29:59.655: E/AndroidRuntime(18470): java.lang.IllegalStateException: Could not         execute method of the activity
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.view.View$1.onClick(View.java:3591)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.view.View.performClick(View.java:4084)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.view.View$PerformClick.run(View.java:16966)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.os.Handler.handleCallback(Handler.java:615)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.os.Handler.dispatchMessage(Handler.java:92)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.os.Looper.loop(Looper.java:137)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.app.ActivityThread.main(ActivityThread.java:4745)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at dalvik.system.NativeStart.main(Native Method)
    02-17 05:29:59.655: E/AndroidRuntime(18470): Caused by: java.lang.reflect.InvocationTargetException
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.lang.reflect.Method.invokeNative(Native Method)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.lang.reflect.Method.invoke(Method.java:511)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.view.View$1.onClick(View.java:3586)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    ... 11 more
    02-17 05:29:59.655: E/AndroidRuntime(18470): Caused by: android.os.NetworkOnMainThreadException
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at libcore.io.IoBridge.connect(IoBridge.java:112)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.net.Socket.startupSocket(Socket.java:566)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at java.net.Socket.<init>(Socket.java:225)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    at com.snivik.morze.LoginWindow.connectToServer(LoginWindow.java:62)
    02-17 05:29:59.655: E/AndroidRuntime(18470):    ... 14 more

1 个答案:

答案 0 :(得分:3)

您的问题是您显然是在主线程中尝试打开套接字,这就是您获得android.os.NetworkOnMainThreadException的原因。

所有网络都必须在后台线程中完成。