使用 RxJava、Retrofit 上传进度 [英] Upload progress with RxJava, Retrofit

查看:92
本文介绍了使用 RxJava、Retrofit 上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用 MVP 设计模式、RxJava、RxAndroid 和 Retrofit 来使用 API 调用.目前,我正在寻找在发送图片时尝试显示上传进度的解决方案.我已经看到了几个可能的实现,但我担心它们不适合我的实现.以下是我使用我提到的库调用 API 的方式:

In my project, I'm using MVP design pattern, RxJava,RxAndroid and Retrofit for consuming the API calls. Currently I am looking for solutions for trying to display an upload progress while I'm sending a picture. I have seen a couple of possible implementations, but I am afraid they do not fit within my implementation. Here's how I consume the API call with the libraries I mentioned:

Subscription subscription = mApiService.modVideoFromVideoController(userHash, videoId, publicVideo, rate)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ModifyVideoResponse>() {
                    @Override
                    public void onCompleted() {
                        Log.d(TAG, "modVideo onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, e.getMessage());
                        Crashlytics.logException(e);
                    }

                    @Override
                    public void onNext(ModifyVideoResponse modifyVideoResponse) {
                        mVideosPresenter.onVideoEdited(position, publicVideo);
                    }
                });
        mCompositeSubscription.add(subscription);

有没有办法做到这一点?

Is there a way to achieve this?

推荐答案

实现此目的的最佳方法是使用 Side Effects 方法.

The best way to achieve this is use Side Effects methods.

对于进度视图隐藏/显示:

使用 doOnSubscribe() 和 doFinally() 绑定这两个事件.这是两个操作符,所以我们可以在逻辑上用 RxCall 绑定它们.

Bind this two events with doOnSubscribe() and doFinally(). This are two operators, so we can logically bind them with RxCall.

doOnSubscribe() 将在您附加到订阅者时被调用.

doOnSubscribe() will be called, when you are attached to Subscriber.

doFinaly() 将在最后被调用.在任何情况下都是成功或错误.

doFinaly() will be called at the end. In any cases Success or Error.

这篇关于使用 RxJava、Retrofit 上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆