Reputation: 5413
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {
}
int result = in.read();
// String result1=in.readLine();
char[] buf = new char[50];
in.read(buf);
String b = new String(buf);
text.setText(b);
I sent the word "hello world"
from the server but what I got back is "ello world"
from the above code . It's missing the first letter h. I used read
instead of readLine
because readLine
doesn't work, it crashed.
Another issue, hello world is displayed in 2 lines instead of one. layout for textview is wrap_content.
Upvotes: 0
Views: 113
Reputation: 3037
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.print("Received string: '");
String inputLine;
String b = "";
while ((inputLine = in.readLine()) != null)
{
b = inputLine;
System.out.println(b);
//or do whatever you want with b
}
With this you will also be able to read multiple lines (in case you reveived more than one)...
I used read instead of readLine because readLine doesn't work, it crashed.
It should not crash...i suggest you should fix this first
Upvotes: 0
Reputation: 8480
This line is consuming the first character:
int result=in.read();
Hence when you do this, buf will not contain it:
in.read(buf);
You can use the mark() and reset() functions on the buffered reader if you need to go back to the beginning. Or otherwise just comment out that line.
Upvotes: 1