Android getInputStream()不会检索所有字节,只是偶尔检索

Android getInputStream()不会检索所有字节,只是偶尔检索,android,httpresponse,Android,Httpresponse,首先,我有一个扩展AsyncTask的类。然后在doInBackground()方法中,我有一些代码发布HTTP post消息并获得响应。此响应被读入字符串,如下所示: DataInputStream dis = new DataInputStream(conn.getInputStream()); byte[] data = new byte[32768]; len = dis.read(data, 0, 32768); if (len > 0) { response = new

首先,我有一个扩展AsyncTask的类。然后在doInBackground()方法中,我有一些代码发布HTTP post消息并获得响应。此响应被读入字符串,如下所示:

DataInputStream dis = new DataInputStream(conn.getInputStream());
byte[] data = new byte[32768];
len = dis.read(data, 0, 32768);

if (len > 0)
{
   response = new String(data, 0, len);
}
此时,响应应该是4127字节(由服务器确认)。五分之一的时间是4127字节。2/5的时间是1205字节,2/5的时间是3853字节。我可以关闭应用程序,重新安装,然后得到相同的变体

管理服务器的人员会记录所有内容,并可以确认一遍又一遍地发送相同的数据。但是Android并没有得到充分的支持。这是无线的,不是3g。当我使用调试器时,我可以看到返回的字符串总是相同的,只是在一个随机点被截断


很抱歉问了这么一个模糊(愚蠢的?)的问题,但我已经看了几个小时了,我看不出有什么问题…

读取方法文档说:从包含的输入流中读取多达len字节的数据到一个字节数组中。尝试读取多达len字节,但可能读取较小的数,可能为零。实际读取的字节数作为整数返回

根据不同的条件,有时您可以读取服务器发送的所有数据,有时会有短时间读取。为了获取所有数据,您应该在循环中不断读取流,直到
len
为-1,表示EOF

InputStream in = ...;
byte[] dst = new byte[32768]; /* Assuming total len is < 32768 */
int idx = 0;
do {
  int len = in.read(dst, idx, 32768 - idx);
  idx += len;
} while (len >= 0 || idx < 32768);
InputStream in=。。。;
字节[]dst=新字节[32768];/*假设总长度小于32768*/
int-idx=0;
做{
int len=in.read(dst,idx,32768-idx);
idx+=len;
}而(len>=0 | | idx<32768);

read方法文档说明:从包含的输入流中读取多达len字节的数据到字节数组中。尝试读取多达len字节,但可能读取较小的数,可能为零。实际读取的字节数作为整数返回

根据不同的条件,有时您可以读取服务器发送的所有数据,有时会有短时间读取。为了获取所有数据,您应该在循环中不断读取流,直到
len
为-1,表示EOF

InputStream in = ...;
byte[] dst = new byte[32768]; /* Assuming total len is < 32768 */
int idx = 0;
do {
  int len = in.read(dst, idx, 32768 - idx);
  idx += len;
} while (len >= 0 || idx < 32768);
InputStream in=。。。;
字节[]dst=新字节[32768];/*假设总长度小于32768*/
int-idx=0;
做{
int len=in.read(dst,idx,32768-idx);
idx+=len;
}而(len>=0 | | idx<32768);