简体   繁体   English

android.os.NetworkOnMainThreadException 当套接字代码似乎在另一个线程上执行时

[英]android.os.NetworkOnMainThreadException when the socket code seems to be executed on another thread

the error错误

 W/System.err: android.os.NetworkOnMainThreadException
    W/System.err:     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)

the code编码

  final Thread receivingserveur = new Thread() {
                                public void run() {
                                    int i=0;
                                    while(i<3) {
                                        try {
                                            System.out.println( "start client" );
                                            sock = new Socket( "90.57.69.108", 1234);
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        try {
                                            sock.setTcpNoDelay( true );
                                        } catch (SocketException e) {
                                            e.printStackTrace();
                                        }



                                        try {
                                            dis = new DataInputStream( sock.getInputStream() );
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        final String message_entrant21;
                                        handler.post( new Runnable() {

                                            @Override


                                            public void run() {


                                                Toast.makeText( getApplicationContext(), "start receiving data", Toast.LENGTH_SHORT ).show();
                                            }
                                        } );
                                        try {
                                            message_entrant21 = bufferedReader.readLine();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        }
                                        handler.post( new Runnable() {

                                            @Override


                                            public void run() {


                                                Toast.makeText( getApplicationContext(), " received data", Toast.LENGTH_SHORT ).show();
                                            }
                                        } );


                                        handler.post( new Runnable() {

                                            @Override


                                            public void run() {

                                                TextView tv = (TextView) findViewById( R.id.textView4 );
                                                tv.setMovementMethod( LinkMovementMethod.getInstance() );
                                                tv.setText( Html.fromHtml( Message232 ) );
                                                Toast.makeText( getApplicationContext(), Message232, Toast.LENGTH_LONG ).show();
                                            }
                                        } );
                                        i += 1;
                                        try {
                                            sock.close();
                                        } catch (IOException e) {
                                            System.out.println( "error to send" );
                                            e.printStackTrace();
                                        }

                                    }


                                }
                            };
                            int i=0;


                            receivingserveur.run();

my questioning我的提问

i wanted to know why when i send some data in a thread loop that for me seams that everything is not on the main thread, so why there his an error that says android.os.NetworkOnMainThreadException when for me ti am on a new thread, may it be that before i execute the serveur thread i execute another thread that use socket that connect but this time sends data?我想知道为什么当我在线程循环中发送一些数据时,对我来说,一切都不是在主线程上,那么为什么当我在一个新线程上时,他会出现android.os.NetworkOnMainThreadException错误,可能是在我执行 serverur 线程之前,我执行了另一个使用套接字连接但这次发送数据的线程? I don't think that it's the case because the other thread have not the same name than this one.我不认为是这种情况,因为另一个线程与这个线程的名称不同。

i think what is the most probable it's that i didn't created the thread loop correctly.我认为最有可能的是我没有正确创建线程循环。

in all this questioning i wanted to know why the socket doesn't connects and puts this error?在所有这些问题中,我想知道为什么套接字无法连接并出现此错误?

If you are not happy with this question please tell me before making an impact.如果您对这个问题不满意,请在产生影响之前告诉我。

使用start()而不是run()来实际启动一个新线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM