Java 错误时自动恢复的HTTP异步下载程序
我的应用程序中有一个异步下载程序,但有时连接会丢失,特别是当我使用移动连接时,如果文件很大(>10MB)。 是否有一种方法可以捕获下载停止的时间,然后强制它恢复并完成下载 这是异步任务Java 错误时自动恢复的HTTP异步下载程序,java,android,download,Java,Android,Download,我的应用程序中有一个异步下载程序,但有时连接会丢失,特别是当我使用移动连接时,如果文件很大(>10MB)。 是否有一种方法可以捕获下载停止的时间,然后强制它恢复并完成下载 这是异步任务doInBackground: protected String doInBackground(String... aurl) { int count; try { URL url = new URL(aurl[0]); URLConnection conexio
doInBackground
:
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
// conexion.setRequestProperty("Range", "bytes=" + downloaded + "-");
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
pesoVideo = lenghtOfFile / 1048576;
output = "/sdcard/" + folderString + "/" + nomeFile + ".mp3";
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(
VideoDownloaderBrowserActivity.this.output);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
这是进程更新的onProgressUpdate
:
protected void onProgressUpdate(String... progress) {
if (Integer.parseInt(progress[0]) > progresso) {
...
}
}
下面是一个API 9下的Android可恢复下载的讨论。否则,对于较新版本也是一个不错的选择
基本上,您需要在服务器上启用字节服务以允许可恢复的下载。您可以使用downloadmanager通过http下载文件。如果连接丢失,它会为您重试下载管理器是一种处理长期运行的HTTP下载的系统服务。以下是参考链接:感谢您的提示,但它将在Android 2.2或更低版本的设备上继续工作?提示:用英语编写代码。这样,你就增加了其他人理解它的机会。尤其是在将其发布到stackoverflow时。