Selaa lähdekoodia

修复KType类型问题

drake 4 vuotta sitten
vanhempi
commit
e907a46d7c

+ 1 - 2
net/src/main/java/com/drake/net/request/BaseRequest.kt

@@ -153,9 +153,8 @@ abstract class BaseRequest {
     /**
      * 为请求附着针对Kotlin的Type信息
      */
-    @OptIn(ExperimentalStdlibApi::class)
     inline fun <reified T> setKType() {
-        okHttpRequest.setKType(typeOf<T>())
+        okHttpRequest.setKType<T>()
     }
 
     //</editor-fold>

+ 8 - 1
net/src/main/java/com/drake/net/request/RequestExtension.kt

@@ -25,7 +25,9 @@ import okhttp.OkHttpUtils
 import okhttp3.FormBody
 import okhttp3.Request
 import java.util.concurrent.ConcurrentLinkedQueue
+import kotlin.jvm.Throws
 import kotlin.reflect.KType
+import kotlin.reflect.typeOf
 
 //<editor-fold desc="请求属性">
 
@@ -118,7 +120,12 @@ fun Request.Builder.setLogRecord(enabled: Boolean) = apply {
  * 设置KType
  */
 fun Request.Builder.setKType(type: KType) = apply {
-    setLabel(type)
+    setLabel(NetLabel.RequestKType(type))
+}
+
+@OptIn(ExperimentalStdlibApi::class)
+inline fun <reified T> Request.Builder.setKType() = apply {
+    setLabel(NetLabel.RequestKType(typeOf<T>()))
 }
 //</editor-fold>
 

+ 4 - 1
sample/src/main/java/com/drake/net/sample/converter/SerializationConverter.kt

@@ -36,7 +36,10 @@ class SerializationConverter(
             when {
                 code in 200..299 -> { // 请求成功
                     val bodyString = response.body?.string() ?: return null
-                    val kType = response.request.kType ?: return null
+                    val kType = response.request.kType ?: throw ConvertException(
+                        response,
+                        "Request does not contain KType"
+                    )
                     return try {
                         val json = JSONObject(bodyString) // 获取JSON中后端定义的错误码和错误信息
                         if (json.getString(this.code) == success) { // 对比后端自定义错误码