簡體   English   中英

自定義模型 Kotlin 中的 Map Retrofit Response

[英]Map Retrofit Response in Custom Model Kotlin

onSuccess() 改造

override fun onSuccess(call: Call<Any?>?, response: Response<Any?>, tag: Any?) {
        when (tag) {
            RequestCodes.API.LOGIN -> {
                val apiResponse = response.body() as? ModelAPIResponse

            val toast = Toast.makeText(
                MainApplication.applicationContext(),
                "Api Success=${apiResponse?.message}",
                Toast.LENGTH_SHORT
            )
            toast.show()}}}

數據庫主體的圖像

在此處輸入圖片說明

Api 響應模型

@SerializedName("statusCode")
var statusCode: String? = null
@SerializedName("message")
var message: String? = null

嘗試讓 Json Response to Object 類像這樣

data class ApiResponse(
   @SerializedName("statusCode")
   var statusCode: String? = null
   @SerializedName("message")
   var message: String? = null
)

然后在你的改造 API 調用上

 @GET("your_endpoint")
    fun getCartListAsync(): Call<ApiResponse>

然后在您成功時將Call<Any>更改為Call<ApiResponse>

我找到了一種映射響應的方法,在編寫映射器的 IOS 中做同樣的事情

  val apiResponse = ModelAPIResponse(response.body() as LinkedTreeMap<String, String>)

然后在數據模型中

class ModelAPIResponse(data: LinkedTreeMap<String, String>) {
var statusCode: String? = null
var message: String? = null

init {
    this.message = data["message"]
    this.statusCode = data["statusCode"]
}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM