0
我正在開發一個簡單的聊天應用程序。 Android客戶端和一個java桌面服務器。我面臨一個問題,即我的客戶端只向服務器發送一條消息。服務器代碼正在Java客戶端上工作,所以只有android代碼有一些問題。 Android客戶端Android客戶端只向java服務器發送一條消息
private static final int SERVERPORT = 5000;
private static final String SERVER_IP = "10.0.2.2";
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new ClientThread()).start();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
EditText et = (EditText) findViewById(R.id.editText1);
public void onClick(View v) {
try {
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
String str = et.getText().toString();
out.println(str);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
class ClientThread implements Runnable {
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);
}
catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
什麼是錯誤? – Max