![](/img/trans.png)
[英]Kotlin extract information from JSON retrofit response and map them to Textview
[英]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.