java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$- Retrofit 2 Android kotlin处为BEGIN_OBJECT

baubqpgj  于 2023-01-28  发布在  Android
关注(0)|答案(2)|浏览(246)

我是Kotlinandroid的新手,我想在我的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
        }
    ]
}

我创建了我的POJO class,如下所示:

data class OrderInQueue(
    var orders: List<Order>
) {
    data class Order(
        var id: String,
        var orderNumber: Int,
        var totalCount: Int,
        var totalPrice: Double
    )
}

下面是我的API Web服务代码:

@GET("api/v1/shopper/orders/feed")
    fun getOrderList (@Header("Authorization") authHeader:String) : Call<List<OrderInQueue>>

主要活动改造代码:

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 $
km0tfn4u

km0tfn4u1#

对象{开始,数组[开头,实际的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>
vaj7vani

vaj7vani2#

你的json响应实际上并没有返回一个订单列表,而只是一个订单......这个订单包含一个订单列表......这就是为什么你的响应只需要一个对象,而不是一个对象列表。

相关问题