简体   繁体   English

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

[英]java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ - Retrofit 2 Android kotlin

I am new to kotlin android. I want to fetch a list of orders in my recyclerView .我是 kotlin android 的新手。我想在我的recyclerView中获取订单列表。 Here is my json response:这是我的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
        }
    ]
}

I have created my POJO class like this:我像这样创建了我的 POJO class

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

Here is my api web service code:这是我的 api web 服务代码:

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

MainActivity Retrofit code: MainActivity Retrofit代码:

 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")
            }


        })
    }

But failed to fetch response with error:但未能获取错误响应:

 java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Objects begin with { and arrays begin with [ the actual response begin with an object so you should change your expected response to an object .对象{开头, arrays[开头,实际responseobject开头,因此您应该将预期response更改为object

Change this:改变这个:

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

to

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

your json response doesn't actually return a list of orders but just an order... And this order contains a list of orders... That's why your response wants only one object and not a list of them.您的 json 响应实际上并没有返回订单列表,而只是一个订单...而此订单包含一个订单列表...这就是为什么您的响应只需要一个 object 而不是它们的列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Retrofit2 java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT - Retrofit2 java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ RetroFit:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY - RetroFit: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ java.lang.IllegalStateException:应为BEGIN_OBJECT,但在第1行第2列$处为BEGIN_ARRAY - java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ Andorid Studio java.lang.IllegalStateException:应为 BEGIN_ARRAY,但在第 1 行第 2 列路径为 BEGIN_OBJECT - Andorid Studio java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ kotlin com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ - kotlin com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在第 1 行第 2 列是 BEGIN_OBJECT - java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 java.lang.IllegalStateException:应为 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ 处为 BEGIN_ARRAY。 如何将 json 转换为 class Object - java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $. How to convert json to class Object java.lang.IllegalStateException:预期 BEGIN_OBJECT 但在第 1 行第 7770 列路径 $.films[8].images.poster 处为 BEGIN_ARRAY - java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 7770 path $.films[8].images.poster com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第2列路径处为BEGIN_ARRAY - com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ Retrofit - java.lang.IllegalStateException:预期BEGIN_ARRAY但是BEGIN_OBJECT - Retrofit - java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM