android - 如何在 Android Studio 中使用 retrofit2 获取 json 日志?

标签 android retrofit2 okhttp

我读了https://futurestud.io/blog/retrofit-2-log-requests-and-responses并成功了。

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

    OkHttpClient client = new OkHttpClient();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.interceptors().add(logging);

    client = builder.build();

    Retrofit retrofit = new Retrofit.Builder()
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(LoginApiService.Login_API_URL)
            .build();

但是它返回的结果是这样的

09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: --> POST http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/ http/1.1
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Type: application/json; charset=UTF-8
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Length: 46
09-08 05:12:24.219 25389-26089/com.example.keepair.myapplication D/OkHttp: --> END POST
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: <-- 400 Bad Request http://ec2-52-78-138-143.ap-northeast-2.compute.amazonaws.com:8000/rest-auth/login/ (96ms)
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Date: Wed, 07 Sep 2016 20:11:27 GMT
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Server: WSGIServer/0.2 CPython/3.4.3
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Vary: Accept
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Allow: POST, OPTIONS
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: X-Frame-Options: SAMEORIGIN
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: Content-Type: application/json
09-08 05:12:24.314 25389-26089/com.example.keepair.myapplication D/OkHttp: <-- END HTTP

这还不够。

因为我使用的是 RESTful api 和 json 类型,

  1. 我想检查我发送给 api 的 json 请求和来自 api 的 json 响应。例如,{ "hi": "bye"},json 请求和响应的完整信息。
  2. 此外,我还想确切地知道我的应用程序通过 header 发送了什么。

我该怎么办?

最佳答案

如果你看一下 HttpLoggingInterceptor.setLevel 的代码:

public HttpLoggingInterceptor setLevel(Level level) {
    if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
    this.level = level;
    return this;
}

您会看到每次调用此方法时都会覆盖上一次调用,因此这样做:

logging.setLevel(HttpLoggingInterceptor.Level.BODY);
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);

将级别设置为 Level.HEADERS
这正是您的日志中发生的事情,您只能看到标题。
只需删除那(第 2 行)行,您就会得到 Level.BODY,这就是您要查找的内容。

关于android - 如何在 Android Studio 中使用 retrofit2 获取 json 日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39378529/

相关文章:

android - NoSuchMethodError Bundle.getString() 方法

android - 在 RxJava 中观察一个基本的自定义事件

android - 为什么 fragment 中的生命周期方法是公开的,而 Activity 的生命周期方法是 protected ?

java - 改造Gson自定义json通用转换器

安卓 : Wireless access point session timeout and HostnameValidation errors

java - 单击 RecyclerView 项时出现 IndexOutOfBoundsException

android - Retrofit2 + RxJava2 Flowable 用于 HTTPs 请求

android - Retrofit 2 发送重复参数

android - CookieManager 是抽象的,无法实例化。为什么?

android - java.lang.NoClassDefFoundError : com. example.api.retrofit.AuthenticationInterceptor$拦截$1