PRDownloader icon indicating copy to clipboard operation
PRDownloader copied to clipboard

How to add any request to queue but download one by one

Open jija opened this issue 7 years ago • 2 comments

I want to add any request to the queue and download one by one but now it downloads as parallel. How I config?

jija avatar Jul 25 '18 07:07 jija

@jija i think you can use downloadId and downloadCompleted event so you can check if your downloadId is completed start another downloadid

ghost1372 avatar Jul 28 '18 15:07 ghost1372

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);

chocolatesoup avatar Aug 26 '18 14:08 chocolatesoup