问题
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
Could you help me to do that
Thanks a lot
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?
Here what I tried,
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.");
}
}
Please could you help me?
Thanks
回答1:
create a callback class,
interface SocketCallback {
void onReceived(String msg);
}
this can be an inner of ClientThread, as they are logically related. that's not required but it's good design.
have your Runnable class accept an instance of this callback in it's constructor.
public ClientThread(ClientThread.SocketCallback cb) {
this.callback = cb;
}
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);
}
});
}
}};
finally, in ClientThread, when you receive a message, call the callback,
while (-1 != val) {
inMsg = in.readLine();
this.callback.onReceived(inMsg);
}
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().
回答2:
You can set the text of a TextView by using the setText() method of it.
Example:
textview_textin.setText(yourText)
By using runOnUiThread() you will be able to access your TextView. See this answer.
来源:https://stackoverflow.com/questions/11147759/android-tcp-client-read-socket-in-a-textview