AndroidAnnotations Rest view response data

隐身守侯 提交于 2019-12-10 18:53:43

问题


I am doing an API call using the Android Annotations RestService. The only problem is that I receive a JSON but I don't know how to see that JSON string. Is there a way I can view the response data so I can see the JSON string/content?

I tried using an ClientHttpRequestInterceptor but that only shows data of the request to the server and not the response.


回答1:


Create this interceptor:

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();
    }
}

And use this interceptor in your client:

@Rest(rootUrl = "your_url", converters = {your converters}, interceptors = LoggingInterceptor.class)
public interface Client {

   // methods
}

Based on this code.



来源:https://stackoverflow.com/questions/30912343/androidannotations-rest-view-response-data

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