Search code examples
androidspringandroid-annotations

AndroidAnnotations Rest view response data


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.


Solution

  • 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.