我想通過這段代碼下載一些xml文件。 當網絡可用時,一切工作正常。當網絡完全不存在時,我已經處理了這個問題。但是現在面臨的問題是,當網絡速度很慢時,下載不會超時。Xml從server.Timeout不能正常工作
超時根本不起作用,我想。如何使它工作?
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]);
這對您有幫助嗎? :http://stackoverflow.com/a/4089528/1405983 –
@Prince不,它檢查互聯網連接。我想檢查慢互聯網。想要取消下載,如果它需要很長時間 – Payal
Okey!只是讀了這個:http://stackoverflow.com/a/11942965/1405983 –