AndroidAnnotations Rest 查看响应数据
AndroidAnnotations Rest view response data
我正在使用 Android Annotations RestService 进行 API 调用。唯一的问题是我收到 JSON 但我不知道如何查看 JSON 字符串。
有没有一种方法可以查看响应数据,以便可以看到 JSON string/content?
我尝试使用 ClientHttpRequestInterceptor,但它只显示对服务器的请求数据,而不显示响应数据。
创建这个拦截器:
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();
}
}
并在您的客户端中使用此拦截器:
@Rest(rootUrl = "your_url", converters = {your converters}, interceptors = LoggingInterceptor.class)
public interface Client {
// methods
}
基于 this 代码。
我正在使用 Android Annotations RestService 进行 API 调用。唯一的问题是我收到 JSON 但我不知道如何查看 JSON 字符串。 有没有一种方法可以查看响应数据,以便可以看到 JSON string/content?
我尝试使用 ClientHttpRequestInterceptor,但它只显示对服务器的请求数据,而不显示响应数据。
创建这个拦截器:
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();
}
}
并在您的客户端中使用此拦截器:
@Rest(rootUrl = "your_url", converters = {your converters}, interceptors = LoggingInterceptor.class)
public interface Client {
// methods
}
基于 this 代码。