如何修复改型错误java.lang.IllegalStateException android
我已经解析了这个链接中的数据 但我也有错误 E/on故障::改型errorjava.lang.IllegalStateException:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$ 下面是我完成的代码如何修复改型错误java.lang.IllegalStateException android,android,kotlin,retrofit,retrofit2,Android,Kotlin,Retrofit,Retrofit2,我已经解析了这个链接中的数据 但我也有错误 E/on故障::改型errorjava.lang.IllegalStateException:应为BEGIN_数组,但在第1行第2列路径处为BEGIN_对象$ 下面是我完成的代码 class RetrofitService { val liveUserResponse:MutableLiveData<List<ContactBase>> = MutableLiveData() companion object
class RetrofitService {
val liveUserResponse:MutableLiveData<List<ContactBase>> = MutableLiveData()
companion object Factory {
var gson = GsonBuilder().setLenient().create()
fun create(): ApiInterface {
Log.e("retrofit","create")
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("https://api.androidhive.info/")
.build()
return retrofit.create(ApiInterface::class.java)
}
}
fun loadContactsData(): MutableLiveData<List<ContactBase>>? {
Log.e("loadAndroidData","yes")
val retrofitCall = create().getContacts()
retrofitCall.enqueue(object : Callback<List<ContactBase>> {
override fun onFailure(call: Call<List<ContactBase>>, t: Throwable?) {
Log.e("on Failure :", "retrofit error"+t)
Log.e("on Failure :", "retrofit error"+call)
}
override fun onResponse(call: Call<List<ContactBase>>, response: retrofit2.Response<List<ContactBase>>) {
val list = response.body()
for (i in list.orEmpty()){
Log.e("on response 1:", ""+i)
}
liveUserResponse.value = list
Log.e("hasActiveObservers 1", liveUserResponse.hasActiveObservers().toString()+" check")
Log.e("on response 2 :", liveUserResponse.toString()+" check")
}
})
return liveUserResponse
}
}
class服务{
val liveUserResponse:MutableLiveData=MutableLiveData()
伴星工厂{
var gson=GsonBuilder().setLenient().create()
fun create():API接口{
Log.e(“改造”、“创建”)
val reformation=reformation.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(“https://api.androidhive.info/")
.build()
返回reformation.create(apinterface::class.java)
}
}
fun loadContactsData():可变LiveData{
Log.e(“loadAndroidData”,“是”)
val call=create().getContacts()
enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃?){
日志e(“故障时:,“改装错误”+t)
Log.e(“故障时:,“改装错误”+调用)
}
覆盖fun onResponse(调用:调用,响应:2.响应){
val list=response.body()
for(list.orEmpty()中的i){
Log.e(“在响应1:”,“+i上)
}
liveUserResponse.value=list
Log.e(“hasactiveobsers1”,liveUserResponse.hasactiveobsers().toString()+“check”)
Log.e(“在响应2:”,liveUserResponse.toString()+“check”)
}
})
返回liveUserResponse
}
}
但它总是处于失败状态
data class ContactBase (val contacts : List<Contacts>)
data class Contacts (
val id : String,
val name : String,
val email : String,
val address : String,
val gender : String,
val phone : Phone
)
data class Phone (
val mobile : String,
val home : String,
val office : String
)
interface ApiInterface{
@GET("contacts/")
fun getContacts(): Call<List<ContactBase>>
}
class AndroidViewModel:ViewModel(){
private val retrofitService = RetrofitService()
fun getContactsData(): MutableLiveData<List<ContactBase>>?{
return retrofitService.loadContactsData()
}
}
数据类ContactBase(val联系人:列表)
数据类联系人(
valid:String,
val name:String,
val电子邮件:String,
val地址:字符串,
瓦尔性别:字符串,
val电话:电话
)
数据类电话(
瓦尔·莫比尔:字符串,
瓦尔·霍姆:弦,
val办公室:字符串
)
接口接口{
@获取(“联系人/”)
fun getContacts():调用
}
类AndroidViewModel:ViewModel(){
专用val翻新服务=翻新服务()
fun getContactsData():可变LiveData{
返回服务。loadContactsData()
}
}
我也交叉验证了url和pojo类。但在改造过程中,往往会出现故障情况 错误表示输入的JSON以
{
开始,而不是[
开始,这意味着它是一个对象,而不是一个对象数组
因此,为了使调用成功,您应该拥有一个包含Contact
数组的类
小提示:因为您使用的是GSON,所以您的模型类需要实现
@SerializedName(string)
变量上方的注释。@饥饿Xhardha谢谢。我为所有pojo类创建了serializedName。但是我已经有了Contact的pojo类,但是如何在成功调用中实现呢?val involutionCall=create().getContacts()//此处仅包装contactbase,但如何获取联系人列表?创建一个cdata类,您可以调用它ContactsResponse
,并在其中添加一个变量val contacts:list,其中包含联系人键的序列化名称。在改装方法中返回call