try {
input = new BufferedReader(new InputStreamReader(
mSocket.getInputStream()));
while (!Thread.currentThread().isInterrupted()) {
String messageStr = null;
messageStr = input.readLine();
if (messageStr != null) {
updateMessages(messageStr, false);
} else {
break;
}
}
input.close();
} catch (IOException e) {
Log.e(CLIENT_TAG, "Server loop error: ", e);
}
我在线程中使用以上代码从套接字连接接收响应。
在Android中,它可以正常工作,因为我使用
out.println()
发送数据,但是当设备连接到ios并开始接收数据时,它无法识别结尾,只有在关闭连接时才能接收到。除了readLine()
以及在以上代码中如何使用以外,是否还有其他替代方法。
最佳答案
这样会更好。
try
{
input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
String messageStr = "";
while (!Thread.currentThread().isInterrupted() && (messageStr = input.readLine()) != null)
{
updateMessages(messageStr, false);
}
input.close();
} catch (Exception e) {
Log.e(CLIENT_TAG, "Server loop error: ", e);
}
关于java - 读取java中的响应结束readLine(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829215/