How can I see Retrofit Error body message? All i see is byte array and I'm having trouble reading it.


How can I see Retrofit Error body message? All i see is byte array and I'm having trouble reading it.


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.
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);
}
@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.