Search code examples
android-studiokotlinandroid-recyclerviewandroid-adapter

Getting this error - java.lang.llegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line1 column 2 path $


I've also tried to do this api calling by using DiffUtil but the error remains the same and i keep getting this error java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ can someone help i'm using kotlin

This is model class

package com.example.beercatalog.model

data class BeerList(
    val list : List<BeerListItem>
    ) {
    data class BeerListItem(
        val alcohol: String,
        val blg: String,
        val brand: String,
        val hop: String,
        val ibu: String,
        val id: Int,
        val malts: String,
        val name: String,
        val style: String,
        val uid: String,
        val yeast: String
    )
}

This is adapter class

package com.example.beercatalog.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.beercatalog.databinding.ListItemBinding
import com.example.beercatalog.model.BeerList

class BeeraAdapter(private val listItem : List<BeerList.BeerListItem>) : RecyclerView.Adapter<BeeraAdapter.ViewHolderBeer>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderBeer {
        val binding = ListItemBinding.inflate(LayoutInflater.from(parent.context),parent ,false)
        return ViewHolderBeer(binding)
    }

    override fun getItemCount(): Int {
      return listItem.size
    }

    override fun onBindViewHolder(holder: ViewHolderBeer, position: Int) {
        val count = listItem[position]
        holder.binding.beerName.text = count.name
        holder.binding.beerBrandName.text = count.brand
        holder.binding.alcoholperc.text = count.alcohol
    }

    class ViewHolderBeer(var binding : ListItemBinding) : RecyclerView.ViewHolder(binding.root){

    }
}

This is api calling in mainactivity

package com.example.beercatalog

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.example.beercatalog.adapter.BeeraAdapter
import com.example.beercatalog.api.BeerRetrofit
import com.example.beercatalog.databinding.ActivityMainBinding
import com.example.beercatalog.model.BeerList
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class MainActivity : AppCompatActivity() {
    lateinit var binding : ActivityMainBinding
    lateinit var adapter : BeeraAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val url = "https://random-data-api.com/api/v2/beers?size=10&response_type=json"
        BeerRetrofit().getInstance()!!
            .beerAPI
            .getBeers(url)
            .enqueue(object : Callback<BeerList>{
                override fun onResponse(call: Call<BeerList>, response: Response<BeerList>) {
                    val beers = response.body()
                    val list  = beers!!.list
                    adapter = BeeraAdapter(list)
                    binding.recyclerView.adapter = adapter

                }

                override fun onFailure(call: Call<BeerList>, t: Throwable) {
                    Toast.makeText(applicationContext, t.localizedMessage, Toast.LENGTH_SHORT)
                        .show()
                }
            })
    }

}

Please explain and solve this error


Solution

  • I believe that the problem is happening in json parsing.

    Following the url you've linked I see actually an array of objects, ie [{...}, {...}].

    However, you're trying to parse BeerList. The parser is looking for a top level json object with a field list in it, which can be represented as array. In other words, the parser is looking for {"list": [{...}, ... {...}]}.

    To fix your issue you need to change getBeers(url) method signature (this should be a retrofit interface). Instead of returning BeerList from it, you need to return List<BeerListItem>.