Browse Source

针对所有http响应,凡是code不是200的都做出提示

songchengcheng 3 months ago
parent
commit
fbbc49d5f0

+ 15 - 0
app/src/main/java/com/sikey/veryfit/component/network/http/converter/CustomBodyConverter.kt

@@ -5,6 +5,12 @@ import android.util.Log
 import com.google.gson.Gson
 import com.sikey.veryfit.component.network.http.BaseResponse
 import com.sikey.veryfit.component.network.http.utils.TypeUtil
+import com.sikey.veryfit.utils.ToastUtils
+import kotlinx.coroutines.DelicateCoroutinesApi
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
 import okhttp3.ResponseBody
 import org.json.JSONObject
 import retrofit2.Converter
@@ -16,6 +22,7 @@ class CustomBodyConverter<T>(private val type: Type) : Converter<ResponseBody, B
         const val TAG = "CustomBodyConverter"
     }
 
+    @OptIn(DelicateCoroutinesApi::class)
     override fun convert(value: ResponseBody): BaseResponse<T> {
         Log.d(TAG, "convert == type: $type; value: $value")
         try {
@@ -28,6 +35,14 @@ class CustomBodyConverter<T>(private val type: Type) : Converter<ResponseBody, B
             model.message = jsonObject.getString("message")
             jsonObject.remove("message")
 
+            if (model.code != "200" && model.message != null) {
+                GlobalScope.launch {
+                    withContext(Dispatchers.Main) {
+                        ToastUtils.showLongToast(model.message)
+                    }
+                }
+            }
+
             val result = jsonObject.toString()
             if (!TextUtils.isEmpty(result) && !"null".equals(result, true)) {
                 model.mData = Gson().fromJson(result, this.type)