【发布时间】:2014-01-20 10:46:15
【问题描述】:
我正在尝试通过此代码下载一些 xml 文件。 当网络可用时一切正常。当网络根本不存在时,我已经处理了这个问题。但是我现在面临的问题是,当网络慢时,下载不会超时。
我认为超时根本不起作用。如何使它起作用?
URL mUrl = new URL(url[i]);
URLConnection conexion = mUrl.openConnection();
response[i] = new DownloadResponse();
response[i].setSuccessful(false);
conexion.connect();
conexion.setConnectTimeout(10000);
// this will be useful so that you can show a typical 0-100% progress bar
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
if (outputFile != null) { // one output file specified in constructor
output = new BufferedOutputStream(new FileOutputStream(
outputFile));
} else if (outputFiles != null) { // an array of output files specified
output = new BufferedOutputStream(new FileOutputStream(
outputFiles[i]));
} else {// no output file specified
output = new BufferedOutputStream(outBytes);
}
InputStream input = new BufferedInputStream(mUrl.openStream());
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
response[i].setSuccessful(true);
response[i].count = i;
response[i].setResponseText(outBytes.toString());
if (total < 32 && outBytes.toString().indexOf("Invalid") < -1){
response[i].setSuccessful(false);
}
output.close();
input.close();
publishProgress(response[i]);
【问题讨论】:
-
这对您有帮助吗? :stackoverflow.com/a/4089528/1405983
-
@Prince 不,它会检查互联网连接。我想检查互联网速度是否慢。如果需要很长时间,想取消下载
-
好吧!只需阅读此内容:stackoverflow.com/a/11942965/1405983
标签: android timeout urlconnection