我第一次创建一种从聊天服务器读取数据的方法时,它冻结了。我发现我的端口号错误,并且在 new BufferedReader(new InputStreamReader(conn.getInputStream()));
有没有办法让我的程序不会因网络错误而冻结?我假设必须有完整的方法
void SendMessage()
{
try {
URL url = new URL("http://50.63.66.138:1044/update");
System.out.println("make connection");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// String line;
String f=new String("");
String line=new String();
while ((line= rd.readLine() ) != null) {
f=f+line;
f+="\n";
}
mUsers.setText(f);
} catch (Exception e) {
System.out.println("exception");
System.out.println(e.getMessage());
}
}
}