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