Kotlin (Retrofit) 错误预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 BEGIN_OBJECT $ [已解决]
Error in Kotlin (Retrofit) Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ [SOLVED]
大家好,我是 Kotlin 的新手,我正在尝试为我的后端实现改造 api 调用。我在 logcat: Expected BEGIN_ARRAY but wis BEGIN_OBJECT at line 1 column 2 path $ 任何建议表示赞赏。
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
@GET("api/users")
fun getUsers(): Call<List<UsersItem>>
}
这是我的数据class:
package com.example.mtaa
data class UsersItem(
val branches: List<Any>,
val id: Int,
val mail: String,
val name: String,
val password: String,
val preferred_branch: String,
val profile_pic: String,
val reservations: List<Any>,
val sub_at: String
)
此代码在 MainActivity
private fun getMyUsers() {
val retrofitBuilder = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
.create(Api::class.java)
val retrofitData = retrofitBuilder.getUsers()
retrofitData.enqueue(object : Callback<List<UsersItem>?> {
override fun onResponse(
call: Call<List<UsersItem>?>,
response: Response<List<UsersItem>?>
) {
val responseBody = response.body()!!
val myStringBuilder = StringBuilder()
for(myData in responseBody){
myStringBuilder.append(myData.name)
myStringBuilder.append("\n")
}
binding.txtId.text = myStringBuilder
}
override fun onFailure(call: Call<List<UsersItem>?>, t: Throwable) {
Log.d("ERROR With BE", "Error:" + t.message)
}
})
}
这是 JSON 我从我的 django 后端得到的
{
"results":[
"id": 1,
"password": "*******",
"name": "Name Surname",
"mail": "mail@mail.xy",
"preferred_branch": "City",
"sub_at": "2022-02-04",
"profile_pic": "path",
"branches": {
"id": 1,
"location": "City",
"gym_capacity": X,
"pool_capacity": X,
"sauna_capacity": X
},
"reservations": []
],
...
}
在您的 Retrofit 服务中,您是说您希望 List
项返回,但您的 API 是 returning 一个包含列表的单个对象它。
您要做的是创建一个名为 UserResults
(或类似名称)的类型,其中包含 results
属性,即 List<UsersItem>
。
data class UserResults(
val results: List<UsersItem>
)
然后,您的 Retrofit 函数可以 return 这个新类型代替:
@GET("api/users")
fun getUsers(): Call<UserResults>
大家好,我是 Kotlin 的新手,我正在尝试为我的后端实现改造 api 调用。我在 logcat: Expected BEGIN_ARRAY but wis BEGIN_OBJECT at line 1 column 2 path $ 任何建议表示赞赏。
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface Api {
@GET("api/users")
fun getUsers(): Call<List<UsersItem>>
}
这是我的数据class:
package com.example.mtaa
data class UsersItem(
val branches: List<Any>,
val id: Int,
val mail: String,
val name: String,
val password: String,
val preferred_branch: String,
val profile_pic: String,
val reservations: List<Any>,
val sub_at: String
)
此代码在 MainActivity
private fun getMyUsers() {
val retrofitBuilder = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
.create(Api::class.java)
val retrofitData = retrofitBuilder.getUsers()
retrofitData.enqueue(object : Callback<List<UsersItem>?> {
override fun onResponse(
call: Call<List<UsersItem>?>,
response: Response<List<UsersItem>?>
) {
val responseBody = response.body()!!
val myStringBuilder = StringBuilder()
for(myData in responseBody){
myStringBuilder.append(myData.name)
myStringBuilder.append("\n")
}
binding.txtId.text = myStringBuilder
}
override fun onFailure(call: Call<List<UsersItem>?>, t: Throwable) {
Log.d("ERROR With BE", "Error:" + t.message)
}
})
}
这是 JSON 我从我的 django 后端得到的
{
"results":[
"id": 1,
"password": "*******",
"name": "Name Surname",
"mail": "mail@mail.xy",
"preferred_branch": "City",
"sub_at": "2022-02-04",
"profile_pic": "path",
"branches": {
"id": 1,
"location": "City",
"gym_capacity": X,
"pool_capacity": X,
"sauna_capacity": X
},
"reservations": []
],
...
}
在您的 Retrofit 服务中,您是说您希望 List
项返回,但您的 API 是 returning 一个包含列表的单个对象它。
您要做的是创建一个名为 UserResults
(或类似名称)的类型,其中包含 results
属性,即 List<UsersItem>
。
data class UserResults(
val results: List<UsersItem>
)
然后,您的 Retrofit 函数可以 return 这个新类型代替:
@GET("api/users")
fun getUsers(): Call<UserResults>