package com.xw.common.net.interceptor import com.xw.common.utils.LogUtils import okhttp3.Headers import okhttp3.Interceptor import okhttp3.MediaType import okhttp3.MultipartBody import okhttp3.Request import okhttp3.Response import okhttp3.ResponseBody import okhttp3.ResponseBody.Companion.toResponseBody import okio.Buffer import java.io.IOException import java.net.URLDecoder class LoggingInterceptor : Interceptor { val TAG = "LoggingInterceptor" @Throws(IOException::class) override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() val startTime = System.currentTimeMillis() val response: Response try { response = chain.proceed(request) } catch (e: IOException) { LogUtils.d(TAG, "----------Start----------------") printRequest(request) val endTime = System.currentTimeMillis() val duration = endTime - startTime e.printStackTrace() LogUtils.e(TAG, "----------End(Error):" + duration + "毫秒----------") throw e // 重新抛出,让上层处理 } LogUtils.d(TAG, "----------Start----------------") printRequest(request) val endTime = System.currentTimeMillis() val bodyString = response.body?.string() ?: "" LogUtils.d(TAG, "response=$bodyString") printHeader("responseHeaders=", response.headers) val duration = endTime - startTime LogUtils.d(TAG, "----------End:" + duration + "毫秒----------") val newBody: ResponseBody = bodyString.toResponseBody(response.body?.contentType()) return response.newBuilder() .body(newBody) .build() } //打印请求体 private fun printRequest(request: Request) { LogUtils.d(TAG, "url=" + request.url) LogUtils.d(TAG, "method=" + request.method) printHeader("headers=", request.headers) if (request.method.compareTo("get", true) == 0 || request.method.compareTo( "delete", true ) == 0 ) { val params = request.url.let { url -> url.queryParameterNames.associateWith { url.queryParameter(it) } }.map { "${it.key}=${it.value}" }.joinToString("&") LogUtils.d(TAG, "params=${params}") } else { try { LogUtils.d(TAG, "params=${URLDecoder.decode(bodyToString(request), "UTF-8")}") } catch (e: Exception) { e.printStackTrace() LogUtils.e(TAG, "params=${bodyToString(request)}") } } } private fun printHeader(prefix: String, headers: Headers) { LogUtils.d(TAG, prefix + buildString { for (i in 0 until headers.size) { val name = headers.name(i) val value = headers.value(i) append(name) append(": ") append(value) append("\n") } }) } private fun bodyToString(request: Request): String? { return try { val copy = request.newBuilder().build() val buffer = Buffer() if (copy.body is MultipartBody) { val multipartBody = copy.body as MultipartBody for (i in 0 until multipartBody.size) { val part = multipartBody.part(i) val mediaType = part.body.contentType() if (!isFilePart(mediaType)) { part.body.writeTo(buffer) } else { buffer.writeUtf8("file params Content-Type: $mediaType\n") } } } else { copy.body!!.writeTo(buffer) } buffer.readUtf8() } catch (e: Exception) { e.printStackTrace() null } } // 是否为文件 fun isFilePart(mediaType: MediaType?): Boolean { if (mediaType != null) { val type = mediaType.toString() return type.startsWith("audio/") || type.startsWith("text/plain") || type.startsWith("image/jpg") || type.startsWith( "application/json" ) } return false } }