android json LogInterceptor打印日志 kotlin代码

目前无法打印上传流里的参数 

@Multipart 类型

其他会打印 json 格式,方便给参数给后台调试

 

 

 

 

 

 代码如下 ::


import android.text.TextUtils
import com.google.gson.GsonBuilder
import me.goldze.mvvmhabit.utils.KLog
import okhttp3.Interceptor
import okhttp3.Response
import okio.Buffer
import java.lang.Exception
import java.net.URLDecoder
import java.nio.charset.Charset
import java.nio.charset.UnsupportedCharsetException
import java.util.concurrent.TimeUnit


/**
 * Description: <\br>
 */
class LogInterceptor : Interceptor {
    // 不对 HTML $ = 进行转义
    val gson = GsonBuilder().disableHtmlEscaping().create()
    private val UTF8 = Charset.forName("UTF-8")
    // 为了打印JSON
    val headerMap = HashMap<String, String>()
    val bodyMap = HashMap<String, String>()

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val requestBody = request.body()
        var body: String? = null
        requestBody?.let {
            val buffer = Buffer()
            requestBody.writeTo(buffer)
            var charset: Charset? = UTF8
            val contentType = requestBody.contentType()
            contentType?.let {
                charset = contentType.charset(UTF8)
            }
            body = buffer.readString(charset!!)
        }
        headerMap.clear()
        bodyMap.clear()
        //---------------
        if (!TextUtils.isEmpty(body)) {
//            val arrayStr = URLDecoder.decode(body, "UTF-8")?.split("&")
            val arrayStr = body?.split("&")
//            val arrayStr = body?.split("&")
            arrayStr?.forEach {
                val nameValue = it.split("=")
                try {
                    //如果是文件流 这里会抛异常
                    bodyMap.put(nameValue[0], URLDecoder.decode(nameValue[1], "UTF-8"))
                } catch (e: Exception) {
                    // 如果是文件流 可能没有KEY
                    if (nameValue.size>1){
                        bodyMap.put(nameValue[0], nameValue[1])
                    }else{
                        bodyMap.put("noKey", nameValue[0])
                    }
                }
            }
        }
        //------------------
        request.headers().let {
            val names = it.names()
            for (name in names) {
                headerMap.put(name, it.get(name) ?: "")
            }
        }
        KLog.i(
            "发送请求: method:" + request.method()
                    + "\nurl:" + request.url()
                    + "\n请求头:" + gson.toJson(headerMap)
//                    + "\n请求参数: " + gson.toJson(body)
                    + "\n请求参数: " + gson.toJson(bodyMap)
        )

        val startNs = System.nanoTime()
        val response = chain.proceed(request)
        val tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs)

        val responseBody = response.body()
        val rBody: String

        val source = responseBody!!.source()
        source.request(java.lang.Long.MAX_VALUE)
        val buffer = source.buffer()

        var charset: Charset? = UTF8
        val contentType = responseBody.contentType()
        contentType?.let {
            try {
                charset = contentType.charset(UTF8)
            } catch (e: UnsupportedCharsetException) {
                KLog.e(e.message)
            }
        }
        rBody = buffer.clone().readString(charset!!)

        KLog.d(
            "收到响应: code:" + response.code()
                    + "\n请求url:" + response.request().url()
                    + "\n请求body:" + gson.toJson(body)
                    + "\nResponse: " + rBody
        )

        return response
    }
}

  

posted on 2019-10-29 16:47  caosiqiao  阅读(775)  评论(0编辑  收藏  举报

导航