1
我环顾四周,发现了这样做的不同方式,所有这些方式似乎都会产生错误。这是我想要运行的。它连接到已经运行的服务器。 关于如何使其工作或更改以使其工作的任何想法?在后台运行线程
String serverAddress = MainActivity.serverAddress;
int port = MainActivity.newport;
Socket socket = new Socket(serverAddress, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
String getName=MainActivity.name;
// Process all messages from server, according to the protocol.
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(getName);
}
else if (line.startsWith("NAMEACCEPTED")) {
//textField.setEditable(true);
}
else if (line.startsWith("MESSAGE")) {
mt.append(line.substring(8) + "\n");
}
}
你在哪里运行这段代码,有哪些错误?一个'AsyncTask'将是一个好主意。在'doInBackground()'中运行网络代码,并用任何其他方法更新'UI'。 – codeMagic
使用线程处理程序或asynctask执行所有网络操作 – skyshine