在实现Runnable时获取NetworkOnMainThreadException

时间:2016-01-24 22:03:14

标签: java android multithreading

我在使用Runnable

时遇到NetworkOnMainThreadException

代码:

public class FullscreenActivity extends AppCompatActivity {
    public Socket socket;
    public int SERVERPORT = 5000;           /* port 5000 (for testing) */
    public String SERVER_IP = "10.0.2.2";   /* local Android address of localhost */
    ViewFlipper flipper;
    ListView listing;

    public void attemptConnect(View view) {
        try {
            EditText editTextAddress = (EditText) findViewById(R.id.address);
            EditText editTextPort = (EditText) findViewById(R.id.port);
            String SERVER_IP_loc = editTextAddress.getText().toString();
            int SERVERPORT_loc = Integer.parseInt(editTextPort.getText().toString());
            System.out.println("Attempt Connect: IP " + SERVER_IP_loc + " Port: " + SERVERPORT_loc);
            System.out.println("SET");
            ClientThread myClientTask = new ClientThread(SERVER_IP_loc, SERVERPORT_loc);
            myClientTask.run();
            System.out.println("CONNECTED");
            flipper.setDisplayedChild(1);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    // Thinks the following is not a thread ??
    class ClientThread implements Runnable {
        ClientThread(String addr, int port) {
            SERVER_IP = addr;
            SERVERPORT = port;
        }

        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
                // gets to this line fine
                socket = new Socket(serverAddr, SERVERPORT); 
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

我的权限设置为允许互联网访问:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

我收到以下错误:

    6188-6188/test W/System.err: android.os.NetworkOnMainThreadException
6188-6188/test W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
6188-6188/test W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
6188-6188/test W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
6188-6188/test W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
6188-6188/test W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
6188-6188/test W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
6188-6188/test W/System.err:     at java.net.Socket.startupSocket(Socket.java:592)
6188-6188/test W/System.err:     at java.net.Socket.<init>(Socket.java:226)
6188-6188/test W/System.err:     at test.FullscreenActivity$ClientThread.run(FullscreenActivity.java:363)
6188-6188/test W/System.err:     at test(FullscreenActivity.java:340)
6188-6188/test W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
6188-6188/test W/System.err:     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(test:270)
6188-6188/test W/System.err:     at android.view.View.performClick(View.java:5198)
6188-6188/test W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
6188-6188/test W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
6188-6188/test W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
6188-6188/test W/System.err:     at android.os.Looper.loop(Looper.java:148)
6188-6188/test W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
6188-6188/test W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
6188-6188/test W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
6188-6188/test W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

据我所知,这应该按预期运作。它似乎认为Runnable不是一个线程,但我无法弄清楚为什么。

1 个答案:

答案 0 :(得分:2)

您需要Thread为您运行Runnable。只需在run上调用Runnable就不会在另一个线程上运行它。

new Thread(new ClientThread(SERVER_IP_loc, SERVERPORT_loc)).start();