3

Im using Single to get some object on success response on retrofit and I'm using SingleObserver to get it. And when I have onSuccess, all works just fine. But to handle onError, SingleObserver hava Throwable, on Response, but I want to get response code, not all error message. How to do this?

@Override
public void getContainerPropertyCombination(ContainerPropertyCombinationListener containerPropertyCombinationListener) {
    formingService.getContainerPropertyCombination(storageHelper.getString(AUTH_TOKEN)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<List<ContainerPropertyCombination>>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onSuccess(List<ContainerPropertyCombination> containerPropertyCombinations) {
            containerPropertyCombinationListener.onSuccess(containerPropertyCombinations);
        }

        @Override
        public void onError(Throwable e) {

        }
    });
}
plase
  • 361
  • 1
  • 3
  • 9
Mingorto
  • 97
  • 8

1 Answers1

4

You can wrap your return type of method defined in your Retrofit interface to Response object. So instead of:

 Single<List<ContainerPropertyCombination>> getContainerPropertyCombination(String token)

you will have:

Single<Response<List<ContainerPropertyCombination>>> getContainerPropertyCombination(String token) 

This way all responses included failed ones will go to onSuccess where you have to decide based on Response.code() if response has failed or not.

Same issue but with Observable: Get response status code using Retrofit 2.0 and RxJava

Héctor
  • 24,444
  • 35
  • 132
  • 243
David Kneys
  • 275
  • 2
  • 7