java - java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT - Retrofit 2 Android kotlin
问题描述
我是 kotlin android 的新手。我想在我的recyclerView
. 这是我的json
回应:
{
"orders": [
{
"id": "be67f572-0305-4c8f-a2c5-d07a10cd9860",
"totalCount": 2,
"totalPrice": 50.16000,
"orderNumber": 1007
},
{
"id": "9a661a3e-8428-4930-bb0e-30a59a7b6e1f",
"totalCount": 5,
"totalPrice": 337.21000,
"orderNumber": 1014
},
{
"id": "59fa6488-7740-43f7-8985-833987ce4a6c",
"totalCount": 5,
"totalPrice": 337.21000,
"orderNumber": 1016
}
]
}
我已经class
像这样创建了我的 POJO:
data class OrderInQueue(
var orders: List<Order>
) {
data class Order(
var id: String,
var orderNumber: Int,
var totalCount: Int,
var totalPrice: Double
)
}
这是我的 api 网络服务代码:
@GET("api/v1/shopper/orders/feed")
fun getOrderList (@Header("Authorization") authHeader:String) : Call<List<OrderInQueue>>
MainActivity 改造代码:
private fun loadOrders() {
//initiate the service
val destinationService = ServiceBuilder.getRetrofitInstance().create(WebService::class.java)
val requestCall = destinationService.getOrderList("Bearer "+sessionManager.GetAccessToken())
//make network call asynchronously
requestCall.enqueue(object : Callback<List<OrderInQueue>> {
override fun onResponse(
call: Call<List<OrderInQueue>>,
response: Response<List<OrderInQueue>>
) {
Log.d("Response", "onResponse: ${response.body()}")
if (response.isSuccessful) {
val orderList = response.body()?.toMutableList()
// Log.d("Response", "countrylist size : ${countryList.size}")
orderRecyclerView.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = orderList?.let { orderAdapter(it, this@MainActivity) }
}
Log.e( "response","Successful ${response.message()}")
// Log.d("Response", "things : ${things}")
// markButtonenble(iv_next)
} else {
Toast.makeText(
this@MainActivity,
"wrong ${response.message()}",
Toast.LENGTH_SHORT
).show()
Log.e( "response","Something went wrong ${response.message()}")
}
}
override fun onFailure(call: Call<List<OrderInQueue>>, t: Throwable) {
Toast.makeText(this@MainActivity, "Something went wrong $t", Toast.LENGTH_SHORT)
.show()
Log.e( "Response","Something went wrong $t")
}
})
}
但未能获取响应并出现错误:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
解决方案
对象以对象开头,{
数组以对象开头,因此您应该将预期更改为对象。[
response
response
改变这个:
@GET("api/v1/shopper/orders/feed")
fun getOrderList (@Header("Authorization") authHeader:String) : Call<List<OrderInQueue>>
到
@GET("api/v1/shopper/orders/feed")
fun getOrderList (@Header("Authorization") authHeader:String) : Call<OrderInQueue>
推荐阅读
- python - pandas groupby 两列并从第二列生成列
- angular - 当我尝试通过whatsapp共享链接时,在离子应用程序中收到消息**共享失败请重试**
- wordpress - Visual-composer 后端编辑器不适用于最新版本的 wordpress(5.0 版)
- ios - 如何在 iOS 的 Numbers 中存储像“2.3.1”这样的值?
- c++ - 我可以使用 QBS 创建用户定义的运行配置文件吗?
- javascript - 淡出不是jquery 3.3.1的功能
- oracle - 缺少右括号-oracle
- docker - 在 docker-compose 中添加wiremock扩展
- mongodb - 在 mongodb 中填充数组
- jquery - 单击时哈希更改