okhttp 输出日志

📅 发布时间:2026/7/5 6:49:40 👁️ 浏览次数:
okhttp 输出日志
/** 可以通过 setLevel 改变日志级别 * 共包含四个级别NONE、BASIC、HEADER、BODY 1 2 3 4 * * NONE 不记录 * * BASIC 请求/响应行 * -- POST /greeting HTTP/1.1 (3-byte body) * -- HTTP/1.1 200 OK (22ms, 6-byte body) * * HEADER 请求/响应行 头 * * -- Host: example.com * Content-Type: plain/text * Content-Length: 3 * * -- HTTP/1.1 200 OK (22ms) * Content-Type: plain/text * Content-Length: 6 * * BODY 请求/响应行 头 体 */ if (!TextUtils.isEmpty(FunctionConfig.HTTP_LOG_LEVEL)) { val logger :HttpLoggingInterceptor.Logger HttpLoggingInterceptor.Logger { message - AppLog.testOnly(OkHttp输出:, message) } val loggingInterceptor: HttpLoggingInterceptor HttpLoggingInterceptor(logger) when (FunctionConfig.HTTP_LOG_LEVEL) { 1 - loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC) 2 - loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS) 3 - loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY) else - loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE) } clientBuilder.addInterceptor(loggingInterceptor) // // 自定义 } Log.i(okhttp输出级别 FunctionConfig.HTTP_LOG_LEVEL) } catch (e: Exception) { e.printStackTrace() }引入com.squareup.okhttp3:logging-interceptor:3.6.0