AndroidAnnotations 的 Rest 客户端日志记录 [英] Rest client logging for AndroidAnnotations

查看:31
本文介绍了AndroidAnnotations 的 Rest 客户端日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AndroidAnnotation 的休息客户端来访问网络服务.我收到以下错误:

I am attempting to use AndroidAnnotation's rest client to access a web service. I am receiving the following error:

org.springframework.http.converter.HttpMessageNotReadableException:
Could not read JSON: Unexpected character ('f' (code 102)):
    was expecting double-quote to start field name

如何让其余客户端记录它收到的实际响应?我无法想象为什么我的 Web 服务会返回这个响应,但除非我能看到完整的响应,否则我无法调试它.我是否必须在 Spring 框架级别设置某种选项?

How can I make the rest client log the actual response it received? I can't imagine why my web service is returning this response, but I can't debug it unless I can see the full response. Do I have to set some kind of option at the level of the Spring framework?

我还想查看我发送的请求的正文.

I would also like to see the body of the request I am sending.

感谢您的帮助!

推荐答案

这里 我们看到 AndroidAnnotations 是 Spring Android RestTemplate Module 的包装器.RestTemplate 的代码是 这里.这样我们就可以找出用于日志记录的 TAG:

Here we see that AndroidAnnotations is a wrapper around the Spring Android RestTemplate Module. The code for the RestTemplate is here. So we can find out which TAG is used for logging:

private static final String TAG = "RestTemplate";

您看不到此 TAG 的日志条目吗?您使用的是哪种转换器/提取器?请贴出调用堆栈.

Are you not able to see log entries for this TAG? Which converter / extractor are you using? Please post the call stack.

wiki 中,他们建议使用 Interceptor 用于记录请求/响应.所以你可以实现自己的拦截器,如:

In the wiki they recommend to use a Interceptor for logging request / response. So you could implement your own interceptor like:

public class LoggingInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] data, ClientHttpRequestExecution execution) throws IOException {
        logRequest(request);
        ClientHttpResponse response = execution.execute(request, data);
        logResponse(response);
        return response;
    }

    private void logRequest(HttpRequest request) {
        // log it
    }

    private void logResponse(ClientHttpResponse response) {
        // log it
    }
}

您在 @Rest 注释中启用拦截器(字段 interceptors).

You enable the interceptor in the @Rest annotation (field interceptors).

这篇关于AndroidAnnotations 的 Rest 客户端日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆