我如何使用带有进度条的改造库?

How can i use retrofit library with progressbar?

我正在使用改造库。在调用回调成功方法之前如何使用进度条?

public void getMovies() {

    MovieClient.getWeather().movies(MovieClient.getRandomMovie(), new Callback<MovieRequest>() {
        @Override
        public void success(MovieRequest movieRequest, Response response) {

            Picasso.with(getApplicationContext()).load(movieRequest.getPoster()).into(image);

        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });
}

你可以这样使用它:

public void getMovies() {

      ProgressDialog mProgressDialog = new ProgressDialog(this);
      mProgressDialog.setIndeterminate(true);
      mProgressDialog.setMessage("Loading...");
      mProgressDialog.show();
      MovieClient.getWeather().movies(MovieClient.getRandomMovie(), new Callback<MovieRequest>() {
      @Override
      public void success(MovieRequest movieRequest, Response response) {

         Picasso.with(getApplicationContext()).load(movieRequest.getPoster()).into(image);

         if (mProgressDialog.isShowing())
            mProgressDialog.dismiss();


    }

      @Override
      public void failure(RetrofitError error) {
          if (mProgressDialog.isShowing())
            mProgressDialog.dismiss();

          Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();

      }
   });
}

您可以查看此方法实用程序。 想法:创建可观察包装器然后添加 2 个操作 "start" & "finish".

public static <T> Observable<T> bindLoadingAction(final Runnable start, final Runnable finish, final Observable<T> source) {
    final Observable<T> o = source.observeOn(AndroidSchedulers.mainThread());
    return o.lift(new Observable.Operator<T, T>() {
        @Override
        public Subscriber<? super T> call(final Subscriber<? super T> child) {
            return new Subscriber<T>() {
                @Override
                public void onStart() {
                    super.onStart();
                    if (start != null) {
                        new Handler(Looper.getMainLooper()).post(start::run);
                    }
                    child.onStart();
                }

                @Override
                public void onCompleted() {
                    if (finish != null) {
                        finish.run();
                    }
                    child.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                    if (finish != null) {
                        finish.run();
                    }
                    child.onError(e);
                }

                @Override
                public void onNext(T t) {
                    if (finish != null) {
                        finish.run();
                    }
                    child.onNext(t);
                }
            };
        }
    });
}

使用:

bindLoadingAction(this::showLoadingView, this::hideLoadingView, YourRetrofitObservable);

(我使用了 Java 8 语法。this::showLoadingView 表示引用当前 class 中的 showLoadingView 方法。)