package com.example.ksswj; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import java.net.UnknownHostException; public class MainActivity extends AppCompatActivity { private EditText ipEditText; private EditText portEditText; private TextView messageTextView; private BufferedReader in; private Socket socket; @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ipEditText = findViewById(R.id.ipEditText); portEditText = findViewById(R.id.portEditText); messageTextView = findViewById(R.id.messageTextView); Button connectButton = findViewById(R.id.connectButton); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String ip = ipEditText.getText().toString(); int port = Integer.parseInt(portEditText.getText().toString()); try { socket = new Socket(ip, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); // 在主页面中的窗口中实时显示消息 new Thread(new Runnable() { @Override public void run() { try { while (true) { String message = in.readLine(); if (message == null) { break; } runOnUiThread(new Runnable() { @Override public void run() { messageTextView.append(message + "\n"); } }); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } @Override protected void onDestroy() { super.onDestroy(); try { if (in != null) { in.close(); } if (socket != null) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } }帮我将代码改成线程执行网络操作
时间: 2024-02-22 16:56:42 浏览: 165
好的,这是修改后的代码:
```
package com.example.ksswj;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity {
private EditText ipEditText;
private EditText portEditText;
private TextView messageTextView;
private BufferedReader in;
private Socket socket;
private Handler handler = new Handler();
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipEditText = findViewById(R.id.ipEditText);
portEditText = findViewById(R.id.portEditText);
messageTextView = findViewById(R.id.messageTextView);
Button connectButton = findViewById(R.id.connectButton);
connectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ip = ipEditText.getText().toString();
int port = Integer.parseInt(portEditText.getText().toString());
new Thread(new Runnable() {
@Override
public void run() {
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// 在主页面中的窗口中实时显示消息
while (true) {
String message = in.readLine();
if (message == null) {
break;
}
handler.post(new Runnable() {
@Override
public void run() {
messageTextView.append(message + "\n");
}
});
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
if (in != null) {
in.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
我将原来的代码包装在一个线程中,以避免在主线程上执行网络操作。我还添加了一个 `Handler` 对象来确保在主线程上更新 UI。
阅读全文