我正在尝试将我的设备连接到本地服务器并发送消息,但在尝试连接时出现此错误:E/Error connect()(2016): android.os.NetworkOnMainThreadException。我的代码是这样的:
//Connect
public boolean Connect() {
//Get data from ip and port from editbox
String IP = ipinput.getText().toString();
int PORT = Integer.valueOf(portinput.getText().toString());
//This is where the error is shown
try {//create socket with IP + PORT values
miCliente = new Socket(IP, PORT);
//If it's connected
if (miCliente.isConnected() == true) {
return true;
} else {
return false;
}
} catch (Exception e) {
//Show error
txtstatus.setTextColor(Color.RED);
txtstatus.setText(" !!! ERROR !!!");
Log.e("Error connect()", "" + e);
return false;
}
}
我已经尝试过 AsyncTask 但也许我做错了而且我是套接字的新手。是的,我的服务器正在运行。谢谢