代码之家  ›  专栏  ›  技术社区  ›  Anton A.

okhttp忽略日志截取程序

  •  1
  • Anton A.  · 技术社区  · 7 年前

    我面临着奇怪的问题。 okhttp给我这样的日志:

    i/system.out:[okhttp]发送请求>>

    i/system.out:[okhttp]发送请求<<

    但我声明在客户端初始化中记录侦听器。

    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
    
    OkHttpClient.Builder client = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(chain -> {
                Request original = chain.request();
                Request.Builder builder = original.newBuilder()
                        .header("Authorization", Credentials.basic(config.login(), config.password()));
                Request request = builder.build();
                return chain.proceed(request);
            })
            .addInterceptor(httpLoggingInterceptor);
    
    return new Retrofit.Builder()
            .baseUrl(config.getServer())
            .callbackExecutor(Executors.newSingleThreadExecutor())
            .addConverterFactory(getMoshiConverterFactory())
            .client(client.build())
            .build();
    

    还有我的 梯度 包含:

    implementation 'com.squareup.retrofit2:retrofit:2.3.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.3.0'
    implementation 'com.squareup.moshi:moshi:1.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'
    implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.7.0'
    

    我做错什么了吗?

    3 回复  |  直到 7 年前
        1
  •  2
  •   SproutinGeek    7 年前
    HttpLoggingInterceptor logger = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("OkHttp", message);
            }
        });
    

    试试这边。

        2
  •  2
  •   Stephan    7 年前

    您可以将其用作网络侦听器,而不是应用程序侦听器。做 .addNetworkInterceptor(httpLoggingInterceptor); 而不是 .addInterceptor(httpLoggingInterceptor);

    这可以让您“观察数据,就像它将通过网络传输一样”。

    查看更多信息 in the wiki pages.

        3
  •  0
  •   user229044    7 年前

    经过一段时间之后,我想象了什么样的日志记录了这个索尼设备展示,并制作了一个副本。 okhttp-logging-interceptor 我在哪里替换

    Platform.get().log(INFO, message, null);
    

    具有

    Log.i("Retrofit", message)
    

    因为设备秀 System.out ,请 Log.i 我是说, Log.w Log.e 来自应用程序的日志。