PRDownloader
PRDownloader copied to clipboard
How to add any request to queue but download one by one
I want to add any request to the queue and download one by one but now it downloads as parallel. How I config?
@jija i think you can use downloadId and downloadCompleted event so you can check if your downloadId is completed start another downloadid
Here's how you can download a list of files, one by one:
int downloadFiles(final String[] urls, int index){
String url = urls[index];
String fileName = url.substring(url.lastIndexOf('/')+1);
String dirPath = getFilesDir().getAbsolutePath() + File.separator + "downloads";
return /*download id*/ PRDownloader.download(url, dirPath, fileName)
.build()
.start(new OnDownloadListener() {
@Override
public void onDownloadComplete() {
// download next file
if (index+1 < urls.length)
downloadFiles(urls, index+1);
else
log("All files downloaded!");
}
@Override
public void onError(Error error) {
log("onError: " +error.toString());
}
});
}
And here's an example how to call it:
String filename1 = "file1.txt";
String filename2 = "file2.txt";
String filename3 = "file3.txt";
String url = "https://www.your-url-goes-here.com/files/";
String[] urls = {
url+filename1,
url+filename2,
url+filename3
};
downloadFiles(urls, 0);