ProgressDalog在Retrofit + RxJava

时间:2017-09-20 20:42:54

标签: android retrofit rx-java

我在使用Retrofit + RxJava发出Rest请求之前使用了ProgressDialog,请求的响应很大,这冻结了ProgressDialog的动画。我怎样才能解决这个问题?我只找到了将runOnUiThreaddoInBackgroundAsyncTask一起使用的示例,但我正在使用RxJava。我尝试了runOnUiThread但它没有用。

//My request
public void getMyData(final MyListener listener) {
AppApi AppApi = getInstanceMyApi();

        AppApi.getMyData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseData>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                       //error
                    }

                    @Override
                    public void onNext(ResponseData response) {
                        //success, send data to presenter to update view
                    }
                });

//Presenter call ws
public void attemptGetDataFromWS() {
    showProgress();
    getMyData(this);
}

@Override
public void onGetMyDataSuccess(ResponseData response) {
     hideProgress();
}

@Override
public void onGetMyDataError(String error) {
     hideProgress();
}

2 个答案:

答案 0 :(得分:0)

public void getMyData(final MyListener listener) {
AppApi AppApi = getInstanceMyApi();

        AppApi.getMyData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<ResponseData>() {
                    @Override
                    public void onCompleted() {
                         dispose();
                    }

                    @Override
                    public void onError(Throwable e) {
                       listener.onGetMyDataError(e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseData response) {
                        listener.onGetMyDataSuccess(response);
                    }
                });

//Presenter call ws
public void attemptGetDataFromWS() {
    showProgress();
    getMyData(this);
}

@Override
public void onGetMyDataSuccess(ResponseData response) {
     hideProgress();
}

@Override
public void onGetMyDataError(String error) {
     hideProgress();
}

答案 1 :(得分:-1)

请遵循此方法。这是使用REST API + Retrofit调用示例

count(*)