How can I see Retrofit Error body message? All i see is byte array and I'm having trouble reading it.
Asked
Active
Viewed 1,344 times
4 Answers
0
You can get the response body in RetrofitError object. err.getResponse()
will give you the error and err.getKind()
will give you the type of error.

Pdksock
- 1,042
- 2
- 13
- 26
0
You can convert those byte array into string.
@Override
public void failure(RetrofitError error) {
String body = new String(error.getResponse().getBody());
System.out.println(body);
}

Fadils
- 1,508
- 16
- 21
0
@Medo, you can do something like this in your code:
public Throwable processError(RetrofitError cause) {
Response r = cause.getResponse();
if (r != null) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(r.getBody().in()));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException", e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
Log.e(TAG, "IOException", e);
}
}
}
if (r.getStatus() == 401) {
Log.e(TAG, "401 Unauthorized Exception...");
} else if (r.getStatus() == 500) {
Log.e(TAG, "500 Server Error...");
}
Log.e(TAG, sb.toString());
}
return cause;
}
Then you can figure out what is causing errors in your code. This is for Retrofit 1.9.0 and not the new 2.x beta version.

Ray Hunter
- 15,137
- 5
- 53
- 51