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

huangapple go评论48阅读模式
英文:

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

问题

我也尝试过使用DiffUtil进行API调用,但错误仍然存在,我不断收到此错误:java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $,有人可以帮助吗?我正在使用Kotlin。

这是模型类:

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

这是适配器类:

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

    // ... (其余部分保持不变)
}

这是MainActivity中的API调用:

class MainActivity : AppCompatActivity() {
    // ... (其余部分保持不变)

    override fun onResponse(call: Call<BeerList>, response: Response<BeerList>) {
        val beers = response.body()
        val list = beers?.list
        adapter = BeeraAdapter(list ?: emptyList())
        binding.recyclerView.adapter = adapter
    }

    // ... (其余部分保持不变)
}
英文:

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&lt;BeerListItem&gt;
    ) {
    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&lt;BeerList.BeerListItem&gt;) : RecyclerView.Adapter&lt;BeeraAdapter.ViewHolderBeer&gt;() {



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

                }

                override fun onFailure(call: Call&lt;BeerList&gt;, t: Throwable) {
                    Toast.makeText(applicationContext, t.localizedMessage, Toast.LENGTH_SHORT)
                        .show()
                }
            })
    }

}

Please explain and solve this error

答案1

得分: 1

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 an 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>.

英文:

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 {&quot;list&quot;: [{...}, ... {...}]}.

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&lt;BeerListItem&gt;.

huangapple
  • 本文由 发表于 2023年5月6日 23:13:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定