2

I am new to kotlin android. I want to fetch a list of orders in my recyclerView. Here is my json response:

{
    "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:

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:

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

MainActivity Retrofit code:

 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 $

2 Answers 2

5

Objects begin with { and arrays begin with [ the actual response begin with an object so you should change your expected response to an 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>
Sign up to request clarification or add additional context in comments.

1 Comment

i also tried the same solution but its giving error in response body
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.