AndroidAnnotations Rest视图响应数据

时间:2015-06-18 10:04:42

标签: android spring android-annotations

我正在使用Android Annotations RestService进行API调用。唯一的问题是我收到了JSON,但我不知道如何看到JSON字符串。 有没有办法可以查看响应数据,以便查看JSON字符串/内容?

我尝试使用ClientHttpRequestInterceptor,但只显示了对服务器的请求数据,而不是响应。

1 个答案:

答案 0 :(得分:3)

创建此拦截器:

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代码。