[英]Android socket app crash
I want my app to connect to a server. 我希望我的应用程序连接到服务器。 I only want the client.
我只想要客户。
protected void onCreate(Bundle savedInstanceState) {
//...
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
//...
} catch (IOException e1) {
//...
}
}
But the application just crashes. 但是应用程序崩溃了。 I started this activity by pressing a button.
我通过按下按钮开始了此活动。 Do you know what the problem could be?
您知道可能是什么问题吗?
You need to perform all your blocking processes in a Thread, and release the main UI Thread, for example: 您需要在线程中执行所有阻止过程,并释放主UI线程,例如:
protected void onCreate(Bundle savedInstanceState) {
//...
new Thread(){
public run(){
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
} catch (UnknownHostException e1) {
//...
} catch (IOException e1) {
//...
}
}
}.start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.