Rest client logging for AndroidAnnotations

淺唱寂寞╮ 提交于 2019-12-24 10:41:34

问题


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

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.

Thanks for your help!


回答1:


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";

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

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
    }
}

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




回答2:


While I do not use AndroidAnnotations and cannot answer your question directly, I would like to propose an alternative solution. You could use a great little utility program called Fiddler. It can do wonders for debugging networking activity, whether it be requests, responses, HTTP headers or practically anything else that would matter in a REST API communication.

You can find a full tutorial on how to setup your environment to use Fiddler here, but to name a few crucial steps (credit goes to the linked page, you can also find helpful pictures there)

Setup Fiddler:

  1. Click menu Tools | Fiddler Options, then select the Connections tab
  2. Make note of the “Fiddler listens on” port (normally it’s 8888)
  3. Make sure the check box for “Allow remote computer to connect” is checked
  4. Switch to the HTTPS tab
  5. Make sure the check boxes for “Capture HTTPS Connects” and “Decrypt HTTPS traffic” are both checked
  6. Restart Fiddler
  7. Make note of the PC’s IP address Close non essential apps on the Windows PC (to minimize web traffic being routed through Fiddler)

Setup your device:

  1. Tap on Settings, then Wi-Fi
  2. Find the network on which you’re connected (normally the first one listed), then tap and hold Choose Modify network from the pop-up
  3. Scroll down and enable “Show advanced options”
  4. Change “Proxy settings” to Manual
  5. Under “Proxy host name” enter the Windows PC IP address from above
  6. Under “Proxy port” enter the Fiddler port from above (usually 8888)
  7. Tap Save and wait a moment for the network to reconnect

Now you will see all the needed details for your REST API calls which makes debugging much easier.



来源:https://stackoverflow.com/questions/24493615/rest-client-logging-for-androidannotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!