Android TCP读了几行

时间:2011-08-12 08:37:29

标签: android sockets tcp bufferedreader

Android TCP套接字客户端是否读了一行响应?

inputStreamReader = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); 
response = bufferedReader.readLine();
response = bufferedReader.readLine();
Log.i(TAG, "Response :: " + response);

我看不懂两行。因为我的服务器会响应 200 OK \ n 内容.......

内容将每秒流向客户端,我不想每次都连接套接字。可以sbd帮忙??

1 个答案:

答案 0 :(得分:1)

在找到空的新行之前将继续读取的示例:

inputStreamReader = new InputStreamReader(socket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); 
String line = bufferedReader.readLine(); // add first line
while (line != "")
{
    response += line;
    line = bufferedReader.readLine();
}
Log.i(TAG, "Response :: " + response);