AndroidAnnotations Rest View Response Data
I am making an API call using Android Annotations RestService. The only problem is I am getting JSON, but I don't know how to see this JSON string. Is there a way to view the response data so that I can see the JSON string / content?
I tried to use ClientHttpRequestInterceptor, but this only shows the request data to the server, not the response.
+3
source to share
1 answer
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.
+2
source to share