解决方案 »
- 自定义view的ANR问题
- 过年好,请问安卓下使用usb的数字键盘的问题
- 下载Android源代码遇到curl: (52) Empty reply from server,为什么
- 怎样将两个apk打包进一个apk里?
- 【重新贴图提问】videoview过程中的问题?
- 安卓手机1G内存和2G内存,对速度影响大吗?
- 安卓移动应用项目开发最新书籍
- ANDROID开发文档翻译
- Android studio 2.3使用出现的问题——没有getdefault
- 我想问一下大家这一段代码的第一行的View是什么
- 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),就会阻塞了,不知道是不是我理解错误