【问题标题】:java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 13 path $.profile [closed]java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 13 列路径 $.profile [关闭]
【发布时间】:2021-10-15 12:40:53
【问题描述】:

这就是我在这里编写代码的方式。这里有 13 个数据类。我该如何处理这些?我该如何解决这个错误?

我的数据类: //

data class ProfileResponse(


 val call_permit: CallPermit,
    val educations: List<Education>,
    val languages: List<Language>,
    val profile: List<Profile>,
    val skills: List<Skill>,
    val trainings: List<Training>,
    val work_experience: List<WorkExperience>
)

我的 apiServiceClass:

     @GET("call/profile/{postNumber}")
        suspend fun getProfileShow(
            @Path("postNumber") number:String
        ):Response<ProfileResponse>

存储库:

  suspend fun getProfileShow(number: String): Response<ProfileResponse> {
        return apiService.getProfileShow(number)
    }

视图模型类:

 val 
response:MutableLiveData<Response<ProfileResponse>>=MutableLiveData()

    fun getProfileShow(number : String) =viewModelScope.launch{
       val res = repository.getProfileShow(number)
        response.value=res
    }

主类:

 viewModel.getProfileShow(ques.user_id)
        
 viewModel.response.observe(viewLifecycleOwner, Observer { response ->

    if(response.isSuccessful){
         binding.profileSurename.text=response.body()?.toString()
    }else {
         Toast.makeText(requireContext(), response.code(), Toast.LENGTH_SHORT).show()
    }
})

【问题讨论】:

标签: android android-studio kotlin mvvm retrofit


【解决方案1】:

您从响应中获得对象列表。但是你只得到对象。那是你得到一个错误。您必须在对象的响应配置文件中声明。

【讨论】:

  • viewModel.response.observe(viewLifecycleOwner, Observer { response -> if(response.isSuccessful){ val profileShows: List? = response.body()?.profile val currentProfile = profileShows? .get(profileShows.size) binding.profileSurename.text= currentProfile?.username }else { } }) 这就是我尝试的方式。你能告诉我怎么做吗?我不明白。请帮忙。
  • 只使用这个 val profileShows: Profile? = response.body()?.profile
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 2021-10-27
  • 2021-04-28
  • 2022-11-25
  • 1970-01-01
相关资源
最近更新 更多