Esperanz0
Esperanz0

Reputation: 1586

Retrofit. Get response if (!response.isSucessful())

In new retrofit I'm making call and overriding 2 methors onResponse and onFailure.

If retrofit sucesfully parsed response to my Class Model I can get it simple but how can I get server response if (!response.isSucessful()) ?

I see code error. Raw response. Error body. But didn't see response from server. This is my error from server... how to get it from response?

        {  
 "message": "422 Unprocessable Entity", 
  "errors": {
            "lang": [
              "Lang required."
            ],
            "provider": [
              "Provider required."
            ] 
      },  
         "status_code": 422 }

Upvotes: 1

Views: 433

Answers (1)

Amit Shekhar
Amit Shekhar

Reputation: 3195

Using this you can get the error body

  if (response != null && response.errorBody() != null) {
    JSONObject jsonObject = new JSONObject(response.errorBody().string());
    String message = jsonObject.getString("message");
    String errors =  jsonObject.getString("errors");
  }

Upvotes: 3

Related Questions