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("下载完成");
}
});
}
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;
}
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;
}
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);
}
}
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());
}
}
Aggregations