Getting json from retrofit's response errorBody Getting json from retrofit's response errorBody json json

Getting json from retrofit's response errorBody


You are using toString() in GSON's fromJson which is not a JSON content. Replace your toString() as string() which will give you the JSON body.Also make sure to use the string() method only once and save the response in a variable, because it will return empty string if you used it again.


you use string(), not toString()

 ErrorResponse errorResponse = gson.fromJson(                response.errorBody().toString(),                ErrorResponse.class);

to

ErrorResponse errorResponse = gson.fromJson(                response.errorBody().string(),                ErrorResponse.class);