1
0
Эх сурвалжийг харах

日志记录器支持Json请求参数输出

drake 4 жил өмнө
parent
commit
211c18fe11

+ 1 - 1
README.md

@@ -110,7 +110,7 @@ implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
 // 支持自动下拉刷新和缺省页的(可选)
 implementation 'com.github.liangjingkanji:BRV:1.3.15'
 
-implementation 'com.github.liangjingkanji:Net:2.3.7'
+implementation 'com.github.liangjingkanji:Net:2.3.8'
 ```
 
 <br>

+ 6 - 1
kalle/src/main/java/com/yanzhenjie/kalle/Request.java

@@ -62,7 +62,12 @@ public abstract class Request {
      */
     public String getLog() {
         if (log == null) {
-            return copyParams().toString();
+            RequestBody body = body();
+            if (body instanceof StringBody) {
+                return body.toString();
+            } else {
+                return copyParams().toString();
+            }
         } else return log;
     }
 

+ 2 - 0
sample/src/main/java/com/drake/net/sample/base/App.kt

@@ -24,6 +24,7 @@ import com.drake.net.sample.BR
 import com.drake.net.sample.BuildConfig
 import com.drake.net.sample.R
 import com.drake.net.sample.callback.MoshiConvert
+import com.drake.net.sample.callback.NetInterceptor
 import com.drake.statelayout.StateConfig
 import com.scwang.smart.refresh.footer.ClassicsFooter
 import com.scwang.smart.refresh.header.MaterialHeader
@@ -44,6 +45,7 @@ class App : Application() {
         initNet("http://182.92.97.186/") {
             converter(MoshiConvert()) // 自动解析JSON映射到实体类中, 转换器分为全局和单例, 覆盖生效(拦截器允许多个)
             cacheEnabled()
+            addInterceptor(NetInterceptor())
             setLogRecord(BuildConfig.DEBUG) // 日志记录器
             logEnabled = BuildConfig.DEBUG // LogCat异常日志
         }

+ 3 - 2
sample/src/main/java/com/drake/net/sample/callback/NetInterceptor.kt

@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+@file:Suppress("ControlFlowWithEmptyBody")
+
 package com.drake.net.sample.callback
 
 import com.drake.net.tag.REQUEST
@@ -27,13 +29,12 @@ class NetInterceptor : Interceptor {
     override fun intercept(chain: Chain): Response {
         val request = chain.request()
 
-        request.getLog()
-
         val tag = request.tag() as TAG
 
         if (tag.contains(REQUEST)) {
             // 可以打印响应体或者其他逻辑
         }
+
         if (tag.contains(RESPONSE)) {
             // 可以打印请求体或者其他逻辑
         }