kotlin 获取此错误- java.lang.llegalStateException:应为开始_OBJECT,但在第1行第2列路径$处为BEGIN_ARRAY

c9qzyr3d  于 2023-05-07  发布在  Kotlin
关注(0)|答案(1)|浏览(147)

我也尝试过使用DiffUtil来调用这个API,但错误仍然是一样的,我一直得到这个错误java.lang.IllegalStateException:预期为开始_OBJECT,但在第1行第2列路径$中为BEGIN_ARRAY
这是模特课

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

这是适配器类

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

    }
}

这是在mainactivity中的API调用

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

}

请解释并解决此错误

gcxthw6b

gcxthw6b1#

我认为问题发生在json解析中。
在你链接的url之后,我看到了一个对象数组,即[{...}, {...}]
但是,您正在尝试解析BeerList。解析器正在查找一个顶级json对象,其中包含一个字段list,该字段可以表示为数组。换句话说,解析器正在查找{"list": [{...}, ... {...}]}
要解决这个问题,您需要更改getBeers(url)方法签名(这应该是一个改进接口)。您需要返回List<BeerListItem>,而不是从它返回BeerList

相关问题