Search in sources :

Example 6 with Progress

use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.

the class RxFileDownloadActivity method fileDownload2.

@OnClick(R.id.fileDownload2)
public void fileDownload2(View view) {
    Observable.create(new ObservableOnSubscribe<Progress>() {

        @Override
        public void subscribe(@NonNull final ObservableEmitter<Progress> e) throws Exception {
            String etString = etUrl.getText().toString();
            String url = TextUtils.isEmpty(etString) ? Urls.URL_DOWNLOAD : etString;
            // 
            OkGo.<File>get(url).headers("aaa", // 
            "111").params("bbb", // 
            "222").execute(new FileCallback() {

                @Override
                public void onSuccess(Response<File> response) {
                    e.onComplete();
                }

                @Override
                public void onError(Response<File> response) {
                    e.onError(response.getException());
                }

                @Override
                public void downloadProgress(Progress progress) {
                    e.onNext(progress);
                }
            });
        }
    }).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            btnFileDownload2.setText("正在下载中...");
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<Progress>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull Progress progress) {
            String downloadLength = Formatter.formatFileSize(getApplicationContext(), progress.currentSize);
            String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize);
            tvDownloadSize.setText(downloadLength + "/" + totalLength);
            String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed);
            tvNetSpeed.setText(String.format("%s/s", speed));
            tvProgress.setText(numberFormat.format(progress.fraction));
            pbProgress.setMax(10000);
            pbProgress.setProgress((int) (progress.fraction * 10000));
        }

        @Override
        public void onError(@NonNull Throwable e) {
            e.printStackTrace();
            btnFileDownload2.setText("下载出错");
            showToast(e.getMessage());
        }

        @Override
        public void onComplete() {
            btnFileDownload2.setText("下载完成");
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Progress(com.lzy.okgo.model.Progress) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) FileCallback(com.lzy.okgo.callback.FileCallback) NonNull(io.reactivex.annotations.NonNull) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter) OnClick(butterknife.OnClick)

Example 7 with Progress

use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.

the class OkDownload method restore.

/**
 * 从数据库中恢复任务
 */
public static List<DownloadTask> restore(List<Progress> progressList) {
    Map<String, DownloadTask> taskMap = OkDownload.getInstance().getTaskMap();
    List<DownloadTask> tasks = new ArrayList<>();
    for (Progress progress : progressList) {
        DownloadTask task = taskMap.get(progress.tag);
        if (task == null) {
            task = new DownloadTask(progress);
            taskMap.put(progress.tag, task);
        }
        tasks.add(task);
    }
    return tasks;
}
Also used : Progress(com.lzy.okgo.model.Progress) ArrayList(java.util.ArrayList) DownloadTask(com.lzy.okserver.download.DownloadTask)

Example 8 with Progress

use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.

the class OkUpload method restore.

/**
 * 从数据库中恢复任务
 */
public static List<UploadTask<?>> restore(List<Progress> progressList) {
    Map<String, UploadTask<?>> taskMap = OkUpload.getInstance().getTaskMap();
    List<UploadTask<?>> tasks = new ArrayList<>();
    for (Progress progress : progressList) {
        UploadTask<?> task = taskMap.get(progress.tag);
        if (task == null) {
            task = new UploadTask<>(progress);
            taskMap.put(progress.tag, task);
        }
        tasks.add(task);
    }
    return tasks;
}
Also used : UploadTask(com.lzy.okserver.upload.UploadTask) Progress(com.lzy.okgo.model.Progress) ArrayList(java.util.ArrayList)

Example 9 with Progress

use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.

the class DownloadTask method download.

/**
 * 执行文件下载
 */
private void download(InputStream input, RandomAccessFile out, Progress progress) throws IOException {
    if (input == null || out == null)
        return;
    progress.status = Progress.LOADING;
    byte[] buffer = new byte[BUFFER_SIZE];
    BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
    int len;
    try {
        while ((len = in.read(buffer, 0, BUFFER_SIZE)) != -1 && progress.status == Progress.LOADING) {
            out.write(buffer, 0, len);
            Progress.changeProgress(progress, len, progress.totalSize, new Progress.Action() {

                @Override
                public void call(Progress progress) {
                    postLoading(progress);
                }
            });
        }
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(input);
    }
}
Also used : Progress(com.lzy.okgo.model.Progress) BufferedInputStream(java.io.BufferedInputStream)

Example 10 with Progress

use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.

the class UploadTask method run.

@Override
public void run() {
    progress.status = Progress.LOADING;
    postLoading(progress);
    final Response<T> response;
    try {
        // noinspection unchecked
        Request<T, ? extends Request> request = (Request<T, ? extends Request>) progress.request;
        final Call rawCall = request.getRawCall();
        request.uploadInterceptor(new ProgressRequestBody.UploadInterceptor() {

            @Override
            public void uploadProgress(Progress innerProgress) {
                if (rawCall.isCanceled())
                    return;
                if (progress.status != Progress.LOADING) {
                    rawCall.cancel();
                    return;
                }
                progress.from(innerProgress);
                postLoading(progress);
            }
        });
        response = request.adapt().execute();
    } catch (Exception e) {
        postOnError(progress, e);
        return;
    }
    if (response.isSuccessful()) {
        postOnFinish(progress, response.body());
    } else {
        postOnError(progress, response.getException());
    }
}
Also used : Call(okhttp3.Call) Progress(com.lzy.okgo.model.Progress) Request(com.lzy.okgo.request.base.Request) ProgressRequestBody(com.lzy.okgo.request.base.ProgressRequestBody)

Aggregations

Progress (com.lzy.okgo.model.Progress)10 File (java.io.File)5 ArrayList (java.util.ArrayList)4 OnClick (butterknife.OnClick)3 ObservableEmitter (io.reactivex.ObservableEmitter)2 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)2 NonNull (io.reactivex.annotations.NonNull)2 Disposable (io.reactivex.disposables.Disposable)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ResponseBody (okhttp3.ResponseBody)2 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 DividerItemDecoration (com.lzy.demo.base.DividerItemDecoration)1 LzyResponse (com.lzy.demo.model.LzyResponse)1 ServerModel (com.lzy.demo.model.ServerModel)1 FileCallback (com.lzy.okgo.callback.FileCallback)1 StringCallback (com.lzy.okgo.callback.StringCallback)1 ProgressRequestBody (com.lzy.okgo.request.base.ProgressRequestBody)1 Request (com.lzy.okgo.request.base.Request)1