class
LoggerInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain&#
63
;): Response {
val orgRequest = chain!!.request()
val respOnse= chain.proceed(orgRequest)
val body = orgRequest.body()
val sb = StringBuilder()
if
(orgRequest.method() ==
"POST"
&& body is FormBody) {
val body1 = body
for
(i in
0
until body1.size()) {
sb.append(body1.encodedName(i) +
"="
+ body1.encodedValue(i) +
","
)
}
sb.delete(sb.length -
1
, sb.length)
Logger.t(AppConfigs.LOGGER_NET_TAG).d(
"code="
+ response.code() +
"|method="
+ orgRequest.method() +
"|url="
+ orgRequest.url()
+
"\n"
+
"headers:"
+ orgRequest.headers().toMultimap()
+
"\n"
+
"post请求体:{"
+ sb.toString() +
"}"
)
}
else
{
Logger.t(AppConfigs.LOGGER_NET_TAG).d(
"code="
+ response.code() +
"|method="
+ orgRequest.method() +
"|url="
+ orgRequest.url()
+
"\n"
+
"headers:"
+ orgRequest.headers().toMultimap())
}
val respOnseBody= response.body()
val cOntentLength= responseBody!!.contentLength()
val source = responseBody.source()
source.request(java.lang.Long.MAX_VALUE)
val buffer = source.buffer()
var charset = UTF8
val cOntentType= responseBody.contentType()
if
(contentType !=
null
) {
try
{
charset = contentType.charset(UTF8)
}
catch
(e: UnsupportedCharsetException) {
return
response
}
}
if
(contentLength != 0L) {
Logger.t(AppConfigs.LOGGER_NET_TAG).json(buffer.clone().readString(charset))
}
return
response
}
}