gpt4 book ai didi

android - OnSuccess 响应不会在 Kotlin 中使用改造

转载 作者:行者123 更新时间:2023-11-29 15:34:56 25 4
gpt4 key购买 nike

我是 Kotlin 的新手。我试图在改造的帮助下调用 API,URL 向我发送响应,但我在 response.body() 中哪里做错了?值(value)没有到来。

我的 JSON 响应:-

{
"data": {
"type": "accounts"
"attributes": {
"payment-type": "prepaid"
}
},
"included": [
{
"type": "a"
"attributes": {
"msn": "0468874507"
}
},
{
"type": "b"
"attributes": {
"msn": "38593"
}
}
]
}

在 DataApi.kt 中

interface DataApi {

@GET("dbnjg")
fun getDataList(): Call<ArrayList<DataValue>>
}

在 DataValue.kt 中

class DataValue {

@SerializedName("data")
var data: Data? = null

@SerializedName("included")
val included: ArrayList<DataInclude>? = null
}

在 ApiClient.kt 中

fun getApiClient(): Retrofit? {
if (retrofit == null) {
retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).build()
}
return retrofit
}

在 MainActivity 中,我试图获得响应,但无法获得响应。我在哪里遗漏了什么或犯了错误?

主要 Activity .kt

val apiInterface: DataApi = ApiClient().getApiClient()!!.create(DataApi::class.java)

apiInterface.getDataList().enqueue(object : Callback<ArrayList<DataValue>> {
override fun onResponse(call: Call<ArrayList<DataValue>>?, response: Response<ArrayList<DataValue>>?) {
Toast.makeText(baseContext, ""+response?.body()!!, Toast.LENGTH_LONG).show()
dataValue = response?.body()!!
}

override fun onFailure(call: Call<ArrayList<DataValue>>?, t: Throwable?) {

}

})

最佳答案

像这样更改您的接口(interface) DataApi:

interface DataApi {
@GET("dbnjg")
fun getDataList(): Call<DataValue>
}

然后像这样调用你的 api:

 apiInterface.getDataList().enqueue(object : Callback<DataValue> { 
override fun onResponse(call: Call<DataValue>?, response:Response<DataValue>?) {
Toast.makeText(baseContext, ""+response?.body()!!, Toast.LENGTH_LONG).show()
dataValue = response?.body()!!
val includedList=dataValue.included
}

override fun onFailure(call: Call<DataValue>?, t: Throwable?) {

}

})

关于android - OnSuccess 响应不会在 Kotlin 中使用改造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52779221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com