gpt4 book ai didi

AndroidAnnotations Rest 查看响应数据

转载 作者:可可西里 更新时间:2023-10-31 22:03:39 26 4
gpt4 key购买 nike

我正在使用 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代码。

关于AndroidAnnotations Rest 查看响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30912343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com