0

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

enter image description here

enter image description here

Medo
  • 968
  • 1
  • 11
  • 26

4 Answers4

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

when restofit receive response,that use Gson convert response to POJO. you have to create POJO for receive response instead of String object.

example : this

Community
  • 1
  • 1
Telember
  • 455
  • 6
  • 24
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