Java – HTTP async downloader automatically recovered in case of error
•
Java
There is an asynchronous downloader in my application, but sometimes the connection is lost, especially when I am on a mobile connection and the file is large (> 10 MB)
This is an asynchronous task 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,count); } output.flush(); output.close(); input.close(); } catch (Exception e) { } return null; }
This is onprogressupdate:
protected void onProgressUpdate(String... progress) { if (Integer.parseInt(progress[0]) > progresso) { ... } }
Solution
This is a thread to discuss the recoverable download in Android under API 9 Otherwise, downloadmanager is also a good choice for newer versions
Basically, you need to enable byte service on the server to allow recoverable downloads
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码