3

I am doing an API call using the Android Annotations RestService. The only problem is that I receive a JSON but I don't know how to see that JSON string. Is there a way I can view the response data so I can see the JSON string/content?

I tried using an ClientHttpRequestInterceptor but that only shows data of the request to the server and not the response.

Bart Bergmans
  • 4,061
  • 3
  • 28
  • 56
  • Well, this is not easy, because when converting the response to JSON, it does not reads into a string, but reads to JSON step by step from the input stream directly. What you can do is sending the same JSON string from the server, but with text/plain content type. Then just use a String as response type and `StringHttpMessageConverter`. – WonderCsabo Jun 18 '15 at 10:51
  • Or if you do not send a JSON, you can remove your JSON converter, and just read it with `StringHttpMessageConverter`. If you use JSON in the request, you cannot remove the converter when making the request, but you can remove it after it was created, in the interceptor. – WonderCsabo Jun 18 '15 at 10:53
  • I can't change the API calls and I can't remove the JSON converter because the app won't launch anymore then. Does that mean it isn't possible for me at the moment? – Bart Bergmans Jun 18 '15 at 11:04
  • You can remove the converter in the interceptor. But this is only for debugging, isn't it? – WonderCsabo Jun 18 '15 at 11:16
  • Yes, it's only for debugging. How can I remove the converter in the interceptor then? – Bart Bergmans Jun 18 '15 at 11:20
  • You can have a look at what I do to log the response of the rest api here : https://github.com/yDelouis/selfoss-android/blob/master/app%2Fsrc%2Fmain%2Fjava%2Ffr%2Fydelouis%2Fselfoss%2Frest%2FSelfossApiInterceptor.java. I use an interceptor and a wrapper of the HttpResponse. – yDelouis Jun 18 '15 at 11:27
  • Never mind, @yDelouis' answer is much better. Just make sure to reset the stream after reading it, so your converter can also read it. – WonderCsabo Jun 18 '15 at 11:49
  • I added an answer based on @yDelouis' solution. – WonderCsabo Jun 18 '15 at 11:58

1 Answers1

3

Create this interceptor:

public class LoggingInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        ClientHttpResponse response = execution.execute(request, body);

        String responseString = stringOf(response.getBody());
        Log.d("RESPONSE", responseString);

        return response;
    }

    public static String stringOf(InputStream inputStream) {
        inputStream.mark(Integer.MAX_VALUE);
        BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder strBuilder = new StringBuilder();
        String line;
        try {
            while ((line = r.readLine()) != null)
                strBuilder.append(line);
        } catch (IOException ignored) {}
        try {
            inputStream.reset();
        } catch (IOException ignored) {}
        return strBuilder.toString();
    }
}

And use this interceptor in your client:

@Rest(rootUrl = "your_url", converters = {your converters}, interceptors = LoggingInterceptor.class)
public interface Client {

   // methods
}

Based on this code.

WonderCsabo
  • 11,947
  • 13
  • 63
  • 105