BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println(reader.readLine());
为什么我上面读不到数据,而下面的可以?
byte[] buf = new byte[1024];
int len = 0;
InputStream input = socket.getInputStream();
while((len = input.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
我在网上看到好多字符串尽量用缓冲流,可以有时候为什么缓冲流得不到数据,而必须用原生的String,求指教
socketstring
System.out.println(reader.readLine());
为什么我上面读不到数据,而下面的可以?
byte[] buf = new byte[1024];
int len = 0;
InputStream input = socket.getInputStream();
while((len = input.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
我在网上看到好多字符串尽量用缓冲流,可以有时候为什么缓冲流得不到数据,而必须用原生的String,求指教
socketstring
解决方案 »
- Android屏幕自适应问题
- 悬赏,黑屏问题!!
- 请教screenOrientation="nosensor"和configChanges="keyboardHidden|ori
- widget求助
- 关于如何自定义菜单Menu
- 请教android进程的一个问题
- 关于手机RingMode问题
- android工程中的activity_main fragment_main.xml是如何关联的
- 求助:怎么样在Phonegap下,运行AngularJS的代码?
- 求助:ListView里面有个Button,想通过点击这个Button来直接拨打TextView的号码
- 请教这种功能是怎么实现的?
- 需求中出现 星期 的需求,怎么搞
System.out.println(reader.readLine());
这个是按行读取必须有换行符 '\n'InputStream input = socket.getInputStream();
while((len = input.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
这个是字节流的方式,显然能读,读到缓冲区继续循环 直到通道中的流没有了。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
byte[] buffer = new byte[128];
int n = -1;
while ((n = bis.read(buffer)) != -1) {
bos.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bos.toByteArray();
}
能完整的读完IO流。尽量不要用字符流,一行行的读取,需要对方发送\n符号,才能读取。
InputStream input = socket.getInputStream();
while((len = input.read(buf))!=-1){
System.out.println(new String(buf,0,len));
}
能收到数据啊