其他分享
首页 > 其他分享> > AndroidAnnotations Rest查看响应数据

AndroidAnnotations Rest查看响应数据

作者:互联网

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

我尝试使用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,spring,android
来源: https://codeday.me/bug/20191120/2041699.html