1

I am calling one web service using `RxJava2' in android native as follows. The problem here is in case of error third party web service is returning HTTP status code error with the error description in errors object separately.

Now the problem here is as service is returning HTTP status code 422 , rxjava call falls under the onError and due to that i am unable to parse Response object which has errors[] array.

response from service

( HTTP Status code is 422 Unprocessable Entity )

{
    "fitness": [],
    "errors": [
        {
            "code": 409,
            "message": "Conflict",
            "errors": "Activity is already taken",
            "activity_id": "100"
        }
    ]
}

Android Code

fitnessFlowable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .takeUntil(response->response.getSummary().getNext()!=null)
            
            .subscribe(new Subscriber<Response>() {
                @Override
                public void onSubscribe(Subscription s) {

                    s.request(Long.MAX_VALUE);
                }

                @Override
                public void onNext(Response response) {

                    Log.e(TAG, "onNext" );

                    if(response !=null){

                        if(response.getFitness()!=null && response.getFitness().size()!=0){

                            Realm realm = Realm.getDefaultInstance();
                            realm.executeTransactionAsync(new Realm.Transaction() {
                                @Override
                                public void execute(Realm realm) {

                                    realm.copyToRealmOrUpdate(response.getFitness());

                                }
                            }, new Realm.Transaction.OnSuccess() {
                                @Override
                                public void onSuccess() {

                                    Log.i(TAG, "onSuccess , fitness data saved");

                                }
                            }, new Realm.Transaction.OnError() {
                                @Override
                                public void onError(Throwable error) {
                                    Log.i(TAG, "onError , fitness data failed to save"+error.getMessage() );
                                }
                            });
                        }else{

                            Log.i(TAG, "onError , no fitness data available" );


                        }

                    }else{
                        Log.i(TAG, "onError , response is null" );

                    }
                }

                @Override
                public void onError(Throwable t) {


                    Log.e(TAG, "onError" +t.getMessage());
                }

                @Override
                public void onComplete() {

                    Log.e(TAG, "onComplete");
                }
            });
Community
  • 1
  • 1
Hunt
  • 8,215
  • 28
  • 116
  • 256

1 Answers1

1

I believe your problem is already answered in this thread: How do I get Response body when there is an error when using Retrofit 2.0 Observables .

But basically what you need to do is to get an HttpException from the Throwable of the onError, and then get the responseBody from that exception

public void onError(Throwable t) {
  if (t instanceof HttpException) {
     ResponseBody body = ((HttpException) t).response().errorBody();
     // Parse the body using gson
  }
  Log.e(TAG, "onError" +t.getMessage());
}
Peddro
  • 1,105
  • 2
  • 9
  • 19
  • Yes, retrofit doesn't automatically parse your error body, you need to make your own parse for that scenario. – Peddro Jun 25 '18 at 13:17