use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.
the class RxFormUploadActivity method formUpload2.
@OnClick(R.id.formUpload2)
public void formUpload2(View view) {
final ArrayList<File> files = new ArrayList<>();
if (imageItems != null && imageItems.size() > 0) {
for (int i = 0; i < imageItems.size(); i++) {
files.add(new File(imageItems.get(i).path));
}
}
Observable.create(new ObservableOnSubscribe<Progress>() {
@Override
public void subscribe(@NonNull final ObservableEmitter<Progress> e) throws Exception {
//
OkGo.<String>post(Urls.URL_FORM_UPLOAD).tag(//
this).headers("header1", //
"headerValue1").headers("header2", //
"headerValue2").params("param1", //
"paramValue1").params("param2", //
"paramValue2").addFileParams("file", //
files).execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
e.onComplete();
}
@Override
public void onError(Response<String> response) {
e.onError(response.getException());
}
@Override
public void uploadProgress(Progress progress) {
e.onNext(progress);
}
});
}
}).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
btnFormUpload2.setText("正在上传中...");
}
}).observeOn(//
AndroidSchedulers.mainThread()).subscribe(new Observer<Progress>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
addDisposable(d);
}
@Override
public void onNext(@NonNull Progress progress) {
System.out.println("uploadProgress: " + 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();
btnFormUpload2.setText("上传出错");
showToast(e.getMessage());
}
@Override
public void onComplete() {
btnFormUpload2.setText("上传完成");
}
});
}
use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.
the class DownloadListActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download_list);
initToolBar(toolbar, true, "开始下载");
initData();
OkDownload.getInstance().setFolder(Environment.getExternalStorageDirectory().getAbsolutePath() + "/aaa/");
OkDownload.getInstance().getThreadPool().setCorePoolSize(3);
folder.setText(String.format("下载路径: %s", OkDownload.getInstance().getFolder()));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
// 从数据库中恢复数据
List<Progress> progressList = DownloadManager.getInstance().getAll();
OkDownload.restore(progressList);
adapter = new DownloadListAdapter(this);
recyclerView.setAdapter(adapter);
checkSDCardPermission();
}
use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.
the class FormUploadActivity method formUpload.
@OnClick(R.id.formUpload)
public void formUpload(View view) {
ArrayList<File> files = new ArrayList<>();
if (imageItems != null && imageItems.size() > 0) {
for (int i = 0; i < imageItems.size(); i++) {
files.add(new File(imageItems.get(i).path));
}
}
// 拼接参数
//
OkGo.<LzyResponse<ServerModel>>post(Urls.URL_FORM_UPLOAD).tag(//
this).headers("header1", //
"headerValue1").headers("header2", //
"headerValue2").params("param1", //
"paramValue1").params("param2", //
"paramValue2").addFileParams("file", // 这种方式为同一个key,上传多个文件
files).execute(new JsonCallback<LzyResponse<ServerModel>>() {
@Override
public void onStart(Request<LzyResponse<ServerModel>, ? extends Request> request) {
btnFormUpload.setText("正在上传中...");
}
@Override
public void onSuccess(Response<LzyResponse<ServerModel>> response) {
handleResponse(response);
btnFormUpload.setText("上传完成");
}
@Override
public void onError(Response<LzyResponse<ServerModel>> response) {
handleError(response);
btnFormUpload.setText("上传出错");
}
@Override
public void uploadProgress(Progress progress) {
System.out.println("uploadProgress: " + 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));
}
});
}
use of com.lzy.okgo.model.Progress in project okhttp-OkGo by jeasonlzy.
the class FileConvert method convertResponse.
@Override
public File convertResponse(Response response) throws Throwable {
String url = response.request().url().toString();
if (TextUtils.isEmpty(folder))
folder = Environment.getExternalStorageDirectory() + DM_TARGET_FOLDER;
if (TextUtils.isEmpty(fileName))
fileName = HttpUtils.getNetFileName(response, url);
File dir = new File(folder);
IOUtils.createFolder(dir);
File file = new File(dir, fileName);
IOUtils.delFileOrFolder(file);
InputStream bodyStream = null;
byte[] buffer = new byte[8192];
FileOutputStream fileOutputStream = null;
try {
ResponseBody body = response.body();
if (body == null)
return null;
bodyStream = body.byteStream();
Progress progress = new Progress();
progress.totalSize = body.contentLength();
progress.fileName = fileName;
progress.filePath = file.getAbsolutePath();
progress.status = Progress.LOADING;
progress.url = url;
progress.tag = url;
int len;
fileOutputStream = new FileOutputStream(file);
while ((len = bodyStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
if (callback == null)
continue;
Progress.changeProgress(progress, len, new Progress.Action() {
@Override
public void call(Progress progress) {
onProgress(progress);
}
});
}
fileOutputStream.flush();
return file;
} finally {
IOUtils.closeQuietly(bodyStream);
IOUtils.closeQuietly(fileOutputStream);
}
}
use of com.lzy.okgo.model.Progress in project BaseProject by feer921.
the class FileConvert method convertResponse.
@Override
public File convertResponse(Response response) throws Throwable {
String url = response.request().url().toString();
if (TextUtils.isEmpty(folder))
folder = Environment.getExternalStorageDirectory() + DM_TARGET_FOLDER;
if (TextUtils.isEmpty(fileName))
fileName = HttpUtils.getNetFileName(response, url);
File dir = new File(folder);
IOUtils.createFolder(dir);
File file = new File(dir, fileName);
IOUtils.delFileOrFolder(file);
InputStream bodyStream = null;
byte[] buffer = new byte[8192];
FileOutputStream fileOutputStream = null;
try {
ResponseBody body = response.body();
if (body == null)
return null;
bodyStream = body.byteStream();
Progress progress = new Progress();
progress.totalSize = body.contentLength();
progress.fileName = fileName;
progress.filePath = file.getAbsolutePath();
progress.status = Progress.LOADING;
progress.url = url;
progress.tag = url;
int len;
fileOutputStream = new FileOutputStream(file);
while ((len = bodyStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
if (callback == null)
continue;
Progress.changeProgress(progress, len, new Progress.Action() {
@Override
public void call(Progress progress) {
onProgress(progress);
}
});
}
fileOutputStream.flush();
return file;
} finally {
IOUtils.closeQuietly(bodyStream);
IOUtils.closeQuietly(fileOutputStream);
}
}
Aggregations