HTTP状态代码有错误时无法解析响应对象

时间:2018-06-25 12:41:24

标签: android rx-java2

我正在使用android native中的“ RxJava2”调用一项Web服务,如下所示。这里的问题是在第三方Web服务出现错误的情况下,该错误返回HTTP状态代码错误以及错误对象中的错误描述。

现在问题出在服务返回HTTP状态代码422时,rxjava调用落在onError下,并且由于我无法解析具有Response数组的errors[]对象

服务响应

(HTTP状态代码为422无法处理的实体)

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

Android代码

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");
                }
            });

1 个答案:

答案 0 :(得分:1)

我相信您的问题已在以下线程中得到解答:How do I get Response body when there is an error when using Retrofit 2.0 Observables

但是基本上,您需要做的是从onError的Throwable中获取HttpException,然后从该异常中获取responseBody

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());
}