-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Start downloading
Jacksgong edited this page Oct 31, 2016
·
6 revisions
FileDownloader.getImpl().create(url)
.setPath(path)
.setListener(listener)
.addHeader(name1,value1)
.addHeader(name2,value2)
.start();
FileDownloader.getImpl().create(url)
.setPath(path)
.setListener(new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void started(BaseDownloadTask task) {
}
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void blockComplete(BaseDownloadTask task) {
}
@Override
protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
}
@Override
protected void completed(BaseDownloadTask task) {
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
}
@Override
protected void warn(BaseDownloadTask task) {
}
}).start();
To start downloading task queue in serial or parallel.
final FileDownloadListener queueTarget = new FileDownloadListener() {
@Override
protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
}
@Override
protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void blockComplete(BaseDownloadTask task) {
}
@Override
protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
}
@Override
protected void completed(BaseDownloadTask task) {
}
@Override
protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
}
@Override
protected void error(BaseDownloadTask task, Throwable e) {
}
@Override
protected void warn(BaseDownloadTask task) {
}
};
// THE FIRST WAY:
//for (String url : URLS) {
// FileDownloader.getImpl().create(url)
// .setCallbackProgressTimes(0) // why do this? in here i assume do not need for each task callback `FileDownloadListener#progress`,
// we just consider which task will complete. so in this way reduce ipc will be effective optimization
// .setListener(queueTarget)
// .asInQueueTask();
// .enqueue();
//}
//if(serial){
// To form a queue with the same queueTarget and execute them linearly
// FileDownloader.getImpl().start(queueTarget, true);
// }
// if(parallel){
// To form a queue with the same queueTarget and execute them in parallel
// FileDownloader.getImpl().start(queueTarget, false);
//}
// THE SECOND WAY:
final FileDownloadQueueSet queueSet = new FileDownloadQueueSet(downloadListener);
final List<BaseDownloadTask> tasks = new ArrayList<>();
for (int i = 0; i < count; i++) {
tasks.add(FileDownloader.getImpl().create(Constant.URLS[i]).setTag(i + 1));
}
queueSet.disableCallbackProgressTimes(); // Do not need for each task callback `FileDownloadListener#progress`,
// We just consider which task will complete. so in this way reduce ipc will be effective optimization.
// Each task will auto retry 1 time if download fail.
queueSet.setAutoRetryTimes(1);
if (serial) {
// Start downloading in serial order.
queueSet.downloadSequentially(tasks);
// If your tasks are not a list, invoke such following will more readable:
// queueSet.downloadSequentially(
// FileDownloader.getImpl().create(url).setPath(...),
// FileDownloader.getImpl().create(url).addHeader(...,...),
// FileDownloader.getImpl().create(url).setPath(...)
// );
}
if (parallel) {
// Start parallel download.
queueSet.downloadTogether(tasks);
// If your tasks are not a list, invoke such following will more readable:
// queueSet.downloadTogether(
// FileDownloader.getImpl().create(url).setPath(...),
// FileDownloader.getImpl().create(url).setPath(...),
// FileDownloader.getImpl().create(url).setSyncCallback(true)
// );
}
queueSet.start();
For managing the serial task queue dynamically please Refs: FileDownloadSerialQueue.
- Initialization
- Start downloading
- Pause or Stop
- Get the internal stored data
- Customizable Component
- Interface