I have a client server program using sockets, one android tablet is server and the other android is client. it works fine in the first example shown below where i am sending a simple message, but in the second example below i tried to use an if statement and it does not work. I wonder why this is?
receivedCommand is the textView
here is the first example that works with no problems
public String line = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
receivedCommand.setText(line);
}
});
}
now for the second example that does not work when using the if statement
public String line = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
//receivedCommand.setText(line);
if(line.equals("test")){
receivedCommand.setText("test received");
}
}
});
}
Seems all you had to do was
Always
trim()when getting a String from other sources, it might just have whitespace where you don’t expect it and cause the comparison to return false.