gpt4 book ai didi

android - 改造错误:-预期为BEGIN_ARRAY,但在路径$处为BEGIN_OBJECT

转载 作者:行者123 更新时间:2023-12-02 13:16:44 25 4
gpt4 key购买 nike

我正在尝试使用Retrofit和Moshi从API检索几行,但遇到此错误:

Retrofit error:- Expected BEGIN_ARRAY but was BEGIN_OBJECT at path$
我从中请求数据的API端点是:
https://thecodecafe.in/gogrocer-ver2.0/api/top_selling
这是我用于从API请求数据的Retrofit和Moshi的设置代码:
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET

private const val BASE_URL = "https://thecodecafe.in/gogrocer-ver2.0/api/"

val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()

val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()

interface GroceryApiServices {
@GET("top_selling")
fun getProperties():
Call<List<GroceryProperty>>
}

object GroceryApi {
val retrofitServices: GroceryApiServices by lazy { retrofit.create(GroceryApiServices::class.java)}
}
这是我的 View 模型类的逻辑,显示了我想如何检索数据:
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kotlin_developer.grocerysell.network.GroceryApi
import com.kotlin_developer.grocerysell.network.GroceryProperty
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class OverviewViewModel: ViewModel() {

private val _response = MutableLiveData<String>()
val response: LiveData<String>
get() = _response

init {
getGroceryProperties()
}

private fun getGroceryProperties(){
GroceryApi.retrofitServices.getProperties().enqueue(object : Callback<List<GroceryProperty>>{
override fun onFailure(call: Call<List<GroceryProperty>>, t: Throwable) {
_response.value = t.message
}

override fun onResponse(
call: Call<List<GroceryProperty>>,
response: Response<List<GroceryProperty>>
) {
_response.value="Success ${response.body()?.size} Grocery Property arrived"
}

})
}

override fun onCleared() {
super.onCleared()
}
}

最佳答案

这就是我想在我的 View 模型类中检索数据的方式

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kotlin_developer.grocerysell.network.GroceryApi
import com.kotlin_developer.grocerysell.network.GroceryProperty
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class OverviewViewModel: ViewModel() {

private val _response = MutableLiveData<String>()
val response: LiveData<String>
get() = _response

init {
getGroceryProperties()
}

private fun getGroceryProperties(){
GroceryApi.retrofitServices.getProperties().enqueue(object : Callback<List<GroceryProperty>>{
override fun onFailure(call: Call<List<GroceryProperty>>, t: Throwable) {
_response.value = t.message
}

override fun onResponse(
call: Call<List<GroceryProperty>>,
response: Response<List<GroceryProperty>>
) {
_response.value="Success ${response.body()?.size} Grocery Property arrived"
}

})
}

override fun onCleared() {
super.onCleared()
}
}

关于android - 改造错误:-预期为BEGIN_ARRAY,但在路径$处为BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62837201/

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