英文:
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<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
答案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 {"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>.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论