Android Java看似随机奇怪的无序网络/文件输出
我们正在从Web API服务器下载三个文件:file.html、file.css和file.js 文件始终具有相同的名称,并且在下载新文件之前会删除旧文件 我们的问题是,有时在下载3到10次之后,一个、两个或所有文件都会被打乱返回 正如您在下面的代码中所看到的,我们输出输入流的第一个字节 例如,html文件的第一行是:Android Java看似随机奇怪的无序网络/文件输出,java,android,file-io,network-programming,Java,Android,File Io,Network Programming,我们正在从Web API服务器下载三个文件:file.html、file.css和file.js 文件始终具有相同的名称,并且在下载新文件之前会删除旧文件 我们的问题是,有时在下载3到10次之后,一个、两个或所有文件都会被打乱返回 正如您在下面的代码中所看到的,我们输出输入流的第一个字节 例如,html文件的第一行是: 当它失败时,它会给我们一个以以下开头的字符串: ????????????Zmo8.Iu1.s~*hJ~ e]}Do gy#c˺f=K- ]G3{4=yF;Qc
当它失败时,它会给我们一个以以下开头的字符串:
????????????Zmo8.Iu1.s~*hJ~
e]}Do gy#c˺f=K-
]G3{4=yF;Qck,H [1~ھ4c!hJKŵ8o/N }+5eAvɸoQH/ȳdRE4ߝXRHS'q$s6n^d>&-$#[ԑn{ B&\0/L0R)\8K;\Ila?NԘǓ;25I)8*ܵ]* r傋\8
,-!ۡ"y*v;qgQ?/5Abs/oK綇STK ƣ4; +w44ٕS%֊_>5"rE-pm[!\?O]'rky2lC~&R{&Cb
NoҵBԗte!}Y3l̩iTrYvSC4[A_q}#<ý$ndŪ9j[֛>:{2/s1if?k/R^Ă3σ
css文件中也有类似的内容
值得注意的是,每个文件的加扰字符串总是相同的,这意味着字符不是随机的,并且发生了其他事情。
这看起来像是网络或编码问题,但我们已经在其他网络、其他设备和其他web服务提供商上复制了它。这些文件通常是通过HTTPS提供的,但我们已经尝试从其他来源获取它们,并得到了相同的结果
我们还有一个iOS应用程序,它在同一台服务器上运行相同的操作,从来没有任何问题
有人知道为什么这些文件看起来是随机乱序的吗
下面是相关的代码摘录
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// download the file
input = connection.getInputStream();
output = new FileOutputStream(getFilesDir().getAbsolutePath() + File.separator + sUrl[1]);
byte data[] = new byte[4096];
long total = 0;
int count;
int i = 0;
while ((count = input.read(data)) != -1) {
if (i == 0) {
String str = new String(data);
System.out.println("----- file " + sUrl[0] + " first line: " + str + "------");
}
i++;
output.write(data, 0, count);
}
我们最终通过隔离应用程序的不同部分找到了原因 我们的问题是gravatar android库与毕加索图像下载库结合使用。它们被用于应用程序的不同部分 似乎它们中的任何一个或我们的实现都有内存泄漏
感谢所有在我们的问题上花费时间的人。您是否尝试过使用其他方法(如wget/curl)复制此问题?我们了解到,此问题已在一个干净的项目中解决,这意味着我们存在内存泄漏或类似问题?我不认为这是问题所在。你愿意把地址贴到这些文件上吗?@quinnjn对不起,我不能这样做。我们目前正在尝试隔离应用程序的各个部分,直到数据提取开始工作。
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
try {
URL url = new URL(sUrl[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
}
// download the file
input = connection.getInputStream();
output = new FileOutputStream(getFilesDir().getAbsolutePath() + File.separator + sUrl[1]);
byte data[] = new byte[4096];
long total = 0;
int count;
int i = 0;
while ((count = input.read(data)) != -1) {
if (i == 0) {
String str = new String(data);
System.out.println("----- file " + sUrl[0] + " first line: " + str + "------");
}
i++;
output.write(data, 0, count);
}