Android的TCP客户端在一个TextView读取插座 [英] Android TCP client read socket in a TextView
问题描述
我开发的Android设备的TcpClient。
我可以连接到一台服务器,我可以跟踪我从服务器接收邮件。
我想显示在服务器上的客户端GUI发送的处理结果的 TextView的textview_textin
I am developing a TCPclient for Android device.
I can connect to a server and I can track that I receive message from the server.
I would like to display the result sent by the server on the client GUI with the TextView textview_textin
你能帮我做到这一点。
非常感谢
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();
}
}
}
});
}
}
非常感谢您的答复。
Thanks a lot for your replies
我可以连接到该设备,但我什么也得不到。我不知道为什么?
I can connect to the device but I receive nothing. I don't know why?
下面是我的尝试,
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
,因为它们有逻辑关系。这不是必须的,但它是很好的设计。
this can be an inner of ClientThread
, as they are logically related. that's not required but it's good design.
有你的的Runnable
类接受这种回调在它的构造函数的一个实例。
have your Runnable
class accept an instance of this callback in it's constructor.
public ClientThread(ClientThread.SocketCallback cb) {
this.callback = cb;
}
当你构建你的的Runnable
( ClientThread
),从你的UI类这样做( JssClientActivity
),这样,
when you construct your Runnable
(ClientThread
), do so from your UI class (JssClientActivity
), like this,
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
,当你收到一条消息,调用回调,
finally, in ClientThread
, when you receive a message, call the callback,
while (-1 != val) {
inMsg = in.readLine();
this.callback.onReceived(inMsg);
}
请注意,你不能从那里ClientThread正在运行的线程更新UI。它必须从UI线程进行更新,因此调用 runOnUiThread()
。
note that you cannot update the UI from the thread where ClientThread is running. it must be updated from the UI thread, hence the call to runOnUiThread()
.
这篇关于Android的TCP客户端在一个TextView读取插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!