Search in sources :

Example 1 with Progress

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("上传完成");
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Progress(com.lzy.okgo.model.Progress) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ArrayList(java.util.ArrayList) StringCallback(com.lzy.okgo.callback.StringCallback) NonNull(io.reactivex.annotations.NonNull) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter) OnClick(butterknife.OnClick)

Example 2 with Progress

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();
}
Also used : Progress(com.lzy.okgo.model.Progress) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) DividerItemDecoration(com.lzy.demo.base.DividerItemDecoration) DefaultItemAnimator(android.support.v7.widget.DefaultItemAnimator)

Example 3 with Progress

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));
        }
    });
}
Also used : LzyResponse(com.lzy.demo.model.LzyResponse) Progress(com.lzy.okgo.model.Progress) ServerModel(com.lzy.demo.model.ServerModel) ArrayList(java.util.ArrayList) File(java.io.File) OnClick(butterknife.OnClick)

Example 4 with Progress

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);
    }
}
Also used : Progress(com.lzy.okgo.model.Progress) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ResponseBody(okhttp3.ResponseBody)

Example 5 with Progress

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);
    }
}
Also used : Progress(com.lzy.okgo.model.Progress) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ResponseBody(okhttp3.ResponseBody)

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