解决方案 »
- SharedPreferences 保存用户设置参数
- Android重力感应之谈
- 写的android例子程序报错:the application has stopped
- http header中的UA问题
- 如何调用 Activity继承下类的方法?
- Android.mk中如何输出信息
- 请教:缓冲区中的音频数据如何播放?
- java.lang.ExceptionInInitializerError,导入的是日历的源码,在添加事件时出现这个问题!求大侠!
- Android RelativeLayout
- Android 怎么利用Socket做在线视频的播放
- Androidannotation 的问题
- 社交app通信数据格式的选择:xml(json) or binary?
Socket socket = new Socket(ip,port);
InputStream ins = socket.getInpuStream();
byte[] b = readInputStream(ins );public static byte[] readInputStream(InputStream ins) {
if (ins == null) {
return null;
}
BufferedInputStream bis = new BufferedInputStream(ins);
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();
} socket是比较稳定的,不太可能出现数据增加的问题,除非服务器主动发送了过多的数据。
这种我开始做的时候就是用这种, while ((n = bis.read(buffer)) != -1) {
bos.write(buffer, 0, n);
}
会导致线程阻塞,当流读取完的时候在执行bis.read(buffer),就会阻塞了,不知道是不是我理解错误