使用okhttp-4.10.0.jar报,但是发现没有HttpLoggingInterceptor的解决方法

发布时间 2023-04-15 12:26:23作者: 施行

HttpLoggingInterceptor 是 OkHttp 库中的一个拦截器,可以用于记录 HTTP 请求和响应的信息,如请求和响应的头部、HTTP 方法和请求体等。在 OkHttp 3.x 版本中,HttpLoggingInterceptor 是内置的,可以直接使用。但是在 OkHttp 4.x 版本中,HttpLoggingInterceptor 被移动到了另外一个库 okhtt-logging-interceptor 中,需要单独引入。

因此,如果你使用的是 OkHttp 4.x 版本,并且想要使用 HttpLoggingInterceptor,你需要在 build.gradle 文件中添加以下依赖:

 

implementation 'com.squareup.okhttp3:logging-interceptor:4.10.0'

 

然后在代码中使用 HttpLoggingInterceptor,示例代码如下:

OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
        .build();

 

注意,这里需要使用 import com.squareup.okhttp3.logging.HttpLoggingInterceptor; 导入 HttpLoggingInterceptor 类。