gpt4 book ai didi

Android TCP 客户端在 TextView 中读取套接字

转载 作者:行者123 更新时间:2023-11-30 04:08:54 27 4
gpt4 key购买 nike

我正在为 Android 设备开发一个 TCP 客户端。我可以连接到服务器,并且可以跟踪我从服务器接收到的消息。我想使用 TextView textview_textin

在客户端 GUI 上显示服务器发送的结果

你能帮我做一下吗

非常感谢

public class JssclientActivity extends Activity {
private EditText serverIp;

private Button connectPhones;

private String serverIpAddress = "192.168.0.2";

private boolean connected = false;

private TextView textview_textin;



public class ClientThread implements Runnable {

public void run() {
try {

InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, 2600);
connected = true;
while (connected) {
try {

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inMsg = "";
char buf[] = null;
int val = in.read();
while (-1 != val) {
inMsg = in.readLine();
}

} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textview_textin = (TextView) findViewById(R.id.textin);
connectPhones = (Button) findViewById(R.id.connect);

connectPhones.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!connected) {
//serverIpAddress = serverIp.getText().toString();
if (!serverIpAddress.equals("")) {
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
}
}
});

}
}

非常感谢您的回复

我可以连接到设备,但我什么也没收到。不知道为什么?

这是我尝试过的,

 public class JssclientActivity extends Activity {
private EditText serverIp;

private Button connectPhones;

private String serverIpAddress = "192.168.20.21";

private boolean connected = false;

private TextView textview_textin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textview_textin = (TextView) findViewById(R.id.textin);
connectPhones = (Button) findViewById(R.id.connect);



ClientThread ct = new ClientThread(new ClientThread.SocketCallback() {
public void onReceived(final String msg) {
JssclientActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
textview_textin.setText(msg);
}
});
}
});

}
}




class ClientThread implements Runnable {

interface SocketCallback {
void onReceived(String msg);
}
private SocketCallback callback;

private boolean connected;

private Socket socket;

public ClientThread(ClientThread.SocketCallback cb) {
this.callback = cb;
try {InetAddress serverAddr = InetAddress.getByName("192.168.20.21");
Log.d("ClientActivity", "C: Connecting...");
//Socket
socket = new Socket(serverAddr, 2600);
connected = true;
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}

public void run() {
while (connected) {
try {

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inMsg = "";
char buf[] = null;
int val = in.read();
// while (-1 != val) {
// inMsg = in.readLine();
// }
while (-1 != val) {
inMsg = in.readLine();
this.callback.onReceived(inMsg);
}

} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
// socket.close();
Log.d("ClientActivity", "C: Closed.");

}
}

你能帮帮我吗?

谢谢

最佳答案

创建一个回调类,

interface SocketCallback {
void onReceived(String msg);
}

这可以是 ClientThread 的内部,因为它们在逻辑上是相关的。这不是必需的,但它是一个很好的设计。

让您的 Runnable 类在其构造函数中接受此回调的实例。

public ClientThread(ClientThread.SocketCallback cb) {
this.callback = cb;
}

当您构建Runnable (ClientThread) 时,从您的 UI 类 (JssClientActivity) 执行此操作,如下所示,

ClientThread ct = new ClientThread(new ClientThread.SocketCallback() {
public void onReceived(String msg) {
JssClientActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
myTextView.setText(msg);
}
});
}
}};

最后,在ClientThread中,当收到消息时,调用回调,

while (-1 != val) {
inMsg = in.readLine();
this.callback.onReceived(inMsg);
}

请注意,您不能从运行 ClientThread 的线程更新 UI。它必须从 UI 线程更新,因此调用 runOnUiThread()

关于Android TCP 客户端在 TextView 中读取套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11147759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com