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 t i 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? 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.
Use start()
and not run()
to actually start a new thread.