从特定标签获取数据作为响应(Kotlin、Retrofit)
Get data from certain tag in response (Kotlin, Retrofit)
我正在尝试通过 Retrofit 和 Kotlin 从 Android 应用进行简单的 GET API 调用。
我正在苦苦挣扎的是从某个响应标签获取数据。
我正在使用带分页的 Django rest 框架,因此结果包含在标记 results
中。我不知道如何查看这个 results
标签 i.o。改造后的整体反应。
我的回复:
{
"count": 13,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"passport_number": 11233546,
"first_name": "Egor",
"last_name": "Wexler",
"email": "string",
"age": 0,
"city": "city"
},
...
{ <other customers> },
]
}
Customer.kt
import com.google.gson.annotations.SerializedName
data class Customer(
@SerializedName("passport_number") val passportNumber: Int,
@SerializedName("first_name") val firstName: String,
@SerializedName("last_name") val lastName: String,
@SerializedName("email") val email: String,
@SerializedName("age") val age: Int,
@SerializedName("city") val city: String
)
ApiInterface.kt
interface ApiInterface {
@GET("customers/")
fun getCustomers() : Call<List<Customer>>
companion object {
fun create() : ApiInterface {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
return retrofit.create(ApiInterface::class.java)
}
}
}
MainActivity.kt
val apiInterface = ApiInterface.create()
apiInterface.getCustomers().enqueue(
object : Callback<List<Customer>> {
override fun onResponse(call: Call<List<Customer>>, response: Response<List<Customer>>) {
// logic for success
}
override fun onFailure(call: Call<List<Customer>>, t: Throwable) {
t.message // java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
}
}
)
所以代码最终转到了 onFailure
以及我在评论中输入的消息。
如果我禁用服务器上的分页 - 它会按预期工作,因为响应会根据应用程序的预期(对象列表 Customer
)。
但我希望应用查看标签结果,因为它实际上包含响应。
我觉得这应该是微不足道的,但找不到方法。
对于标记为“预期 BEGIN_ARRAY 但 BEGIN_OBJECT” 的类似问题,我看到了很多答案,但其中 none 解决了我的问题。
您应该创建一个与您的回复结构相同的回复class。
在你的情况下,可以这样做:
data class PageResult(
val count: Int,
val next: Int?,
val previous: Int?,
val results: List<Customer>
)
并在您的 API 界面中使用:
@GET("customers/")
fun getCustomers() : Call<PageResult>
我正在尝试通过 Retrofit 和 Kotlin 从 Android 应用进行简单的 GET API 调用。
我正在苦苦挣扎的是从某个响应标签获取数据。
我正在使用带分页的 Django rest 框架,因此结果包含在标记 results
中。我不知道如何查看这个 results
标签 i.o。改造后的整体反应。
我的回复:
{
"count": 13,
"next": null,
"previous": null,
"results": [
{
"id": 2,
"passport_number": 11233546,
"first_name": "Egor",
"last_name": "Wexler",
"email": "string",
"age": 0,
"city": "city"
},
...
{ <other customers> },
]
}
Customer.kt
import com.google.gson.annotations.SerializedName
data class Customer(
@SerializedName("passport_number") val passportNumber: Int,
@SerializedName("first_name") val firstName: String,
@SerializedName("last_name") val lastName: String,
@SerializedName("email") val email: String,
@SerializedName("age") val age: Int,
@SerializedName("city") val city: String
)
ApiInterface.kt
interface ApiInterface {
@GET("customers/")
fun getCustomers() : Call<List<Customer>>
companion object {
fun create() : ApiInterface {
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
return retrofit.create(ApiInterface::class.java)
}
}
}
MainActivity.kt
val apiInterface = ApiInterface.create()
apiInterface.getCustomers().enqueue(
object : Callback<List<Customer>> {
override fun onResponse(call: Call<List<Customer>>, response: Response<List<Customer>>) {
// logic for success
}
override fun onFailure(call: Call<List<Customer>>, t: Throwable) {
t.message // java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
}
}
)
所以代码最终转到了 onFailure
以及我在评论中输入的消息。
如果我禁用服务器上的分页 - 它会按预期工作,因为响应会根据应用程序的预期(对象列表 Customer
)。
但我希望应用查看标签结果,因为它实际上包含响应。
我觉得这应该是微不足道的,但找不到方法。
对于标记为“预期 BEGIN_ARRAY 但 BEGIN_OBJECT” 的类似问题,我看到了很多答案,但其中 none 解决了我的问题。
您应该创建一个与您的回复结构相同的回复class。
在你的情况下,可以这样做:
data class PageResult(
val count: Int,
val next: Int?,
val previous: Int?,
val results: List<Customer>
)
并在您的 API 界面中使用:
@GET("customers/")
fun getCustomers() : Call<PageResult>