1

I'm using Retrofit 2.0 in my project with Observable interface and the Result wrapper. The method:

Observable<CompanyModel> companyList(@Query("userAcc") String userAcc,
                                     @Query("password") String password);

The code:

RetrofitHelper.getCompanyList(userName, password).subscribe(new Observer<CompanyModel>() {
        @Override
        public void onCompleted() {
            System.out.println("Observable completed");
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("Observable error");
        }

        @Override
        public void onNext(CompanyModel companyModel) {
            List<Company> company = companyModel.getCompany();
            System.out.println("Observable next");
        }
    });

I want to get response when the error occurred,but onError return Throwable. How can I get the response?

Even2015
  • 39
  • 6
  • In retrofit 1.9, the throwable was actually a RetrofitError, on which you could get the Body like that: `((RetrofitError) e).getBodyAs(CompanyModel.class)`. Not sure how it works with retrofit 2, but I would start by putting a break point in that `onError` and checking more closely what exactly is in that throwable – njzk2 Dec 07 '15 at 02:42
  • @njzk2 The throwable may be SocketTimeoutException,JSONException...The question is that I can get a JSONException ,but I can not get the Response, can not see the return string of JSONObject, can not get the status code – Even2015 Dec 07 '15 at 03:00

1 Answers1

1

I think you can wrap your model inside Response change the Observer declaration to Observable<Response<CompanyModel>> and get the Error errorBody()

For another reference you can check this github issue

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • above one is helpful to me. This is also help full https://stackoverflow.com/questions/33815515/how-do-i-get-response-body-when-there-is-an-error-when-using-retrofit-2-0-observ – Ninja Oct 13 '22 at 12:10