连接到远程TCP插槽时,我的应用程序崩溃

时间:2017-11-10 11:28:26

标签: android sockets tcp networkonmainthread

我已经尝试了几个小时来获取我的应用程序(在Android 7.1.2上运行)连接到家庭局域网中桌面PC上的服务器插槽。

public class MainActivity extends AppCompatActivity {

    static final String SERVER_IP   = "192.168.XXX.XXX"; // X's are replaced by actual IP
    static final int    PORT        = 30000;

    Socket clientSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button btReceive  = (Button) findViewById(R.id.btReceive);

        final TextView txtReceivedText  = (TextView) findViewById(R.id.txtReceivedText);
        final TextView txtStatus        = (TextView) findViewById(R.id.txtStatus);



        btReceive.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                txtStatus.setText("Received button pressed.");

                try {

                    Socket s = new Socket(SERVER_IP, PORT);


                    BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    final String st = input.readLine();

                    txtReceivedText.setText(st);

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

        });

    }


}

执行后,我在Android工作室的控制台上获得输出:

...
Waiting for process to come online
Connected to process 16980 on device samsung-gt_i9515-a72266ed
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.jonas.network_connector, PID: 16980
                  android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
                      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:333)
                      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
                      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
                      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
                      at java.net.Socket.connect(Socket.java:605)
                      at java.net.Socket.connect(Socket.java:554)
                      at java.net.Socket.<init>(Socket.java:431)
                      at java.net.Socket.<init>(Socket.java:210)
                      at com.example.jonas.network_connector.MainActivity$2.onClick(MainActivity.java:88)
                      at android.view.View.performClick(View.java:5637)
                      at android.view.View$PerformClick.run(View.java:22433)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6186)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
I/Process: Sending signal. PID: 16980 SIG: 9
Application terminated.

由于我不熟悉套接字编程(以及android),我不知道如何继续查找错误。

我的清单有权限行:

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

3 个答案:

答案 0 :(得分:2)

  

android.os.NetworkOnMainThreadException

此异常告诉您正在尝试在主线程中调用remote。尝试使用AsyncTask进行调用,问题不应再次出现。 Android操作系统阻止在主线程中执行长时间运行的操作,因为它可以锁定UI。

答案 1 :(得分:2)

您需要在工作线程上进行网络工作。

btReceive.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                txtStatus.setText("Received button pressed.");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Socket s = new Socket(SERVER_IP, PORT);
                            BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                            final String st = input.readLine();
                            txtReceivedText.post(new Runnable(){

                                @Override
                                public void run() {
                                    txtReceivedText.setText(st);
                                }
                            });

                            s.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    }
                }).start();
            });

答案 2 :(得分:1)

  

android.os.NetworkOnMainThreadException

这意味着您的应用程序尝试在其主线程上执行网络操作。在AsyncTask中运行您的代码。

示例:

 class TCPTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }
    }

执行异步任务:

在oncreate()方法中添加此代码

new TCPTask().execute();