java – 错误时自动恢复的HTTP Async Downloader

前端之家收集整理的这篇文章主要介绍了java – 错误时自动恢复的HTTP Async Downloader前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序中有一个异步下载器,但有时连接丢失,尤其是当我在移动连接上并且文件很大(> 10 MB)时.
有没有办法在下载停止时捕获,然后强制它继续完成下载的结果?

这是异步任务doInBackground:

  1. protected String doInBackground(String... aurl) {
  2. int count;
  3.  
  4. try {
  5.  
  6. URL url = new URL(aurl[0]);
  7. URLConnection conexion = url.openConnection();
  8. // conexion.setRequestProperty("Range","bytes=" + downloaded + "-");
  9. conexion.connect();
  10.  
  11. int lenghtOfFile = conexion.getContentLength();
  12. pesoVideo = lenghtOfFile / 1048576;
  13. output = "/sdcard/" + folderString + "/" + nomeFile + ".mp3";
  14. InputStream input = new BufferedInputStream(url.openStream());
  15. OutputStream output = new FileOutputStream(
  16. VideoDownloaderBrowserActivity.this.output);
  17.  
  18. byte data[] = new byte[1024];
  19. long total = 0;
  20.  
  21. while ((count = input.read(data)) != -1) {
  22. total += count;
  23. publishProgress("" + (int) ((total * 100) / lenghtOfFile));
  24. output.write(data,count);
  25. }
  26.  
  27. output.flush();
  28. output.close();
  29. input.close();
  30.  
  31. } catch (Exception e) {
  32. }
  33.  
  34. return null;
  35. }

这是onProgressUpdate:

  1. protected void onProgressUpdate(String... progress) {
  2. if (Integer.parseInt(progress[0]) > progresso) {
  3. ...
  4. }
  5. }

解决方法

这是一个 thread讨论在API 9下面的Android中的可恢复下载.否则 DownloadManager对于较新版本也是一个不错的选择.

基本上,您需要在服务器上启用字节服务以允许可恢复的下载.

猜你在找的Java相关文章