I used code below for receiving messages from a server but it only gets one message like "Hello," but the second message which is "How are you?" is not detected by the application. I tried to fix it but I was not able to.
However, there is not any error.
Here is my code:
new Thread(new Runnable() {
@Override
public void run() {
int available = 0;
while (true) {
try {
available = ClientInPutStream.available();
if (available > 0) {
break;
}
} catch (IOException e) {
msg = e.toString();
showMSG();
}
}
try {
char[] ServerMSG = new char[available];
Reader.read(ServerMSG, 0, available);
StringBuilder sb = new StringBuilder();
sb.append(ServerMSG, 0, available);
msg = sb.toString();
showMSG();
} catch (IOException e) {
msg = e.toString();
showMSG();
}
}
}).start();
Thanks in advance!
EDIT:
I tried code below and I got that need to invoke this thread manually by a button which update text view do you have any solution for this? in order to automate it.
new Thread(new Runnable() {
@Override
public void run() {
byte[] buffer = new byte[128]; // buffer store for the stream
int bytes; // bytes returned from read()
try {
bytes = ClientInPutStream.read(buffer);
byte[] readBuf = buffer;
String strIncom = new String(readBuf, 0, bytes);
msg2+=strIncom;
showmsg();
}catch (Exception e){msg=e.toString();showError();}
}
}).start();