我正在为 Android 设备开发 TCPclient。我可以连接到服务器,并且可以跟踪我从服务器接收到的消息。我想在客户端 GUI 上显示服务器发送的结果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();
}
}
}
});
}
}
非常感谢您的回复
我可以连接到设备,但我什么也没收到。我不知道为什么?
这是我尝试过的,
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.");
}
}
请问你能帮帮我吗?
谢谢