【问题标题】:kotlin com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $kotlin com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ BEGIN_ARRAY
【发布时间】:2020-01-29 22:00:48
【问题描述】:

我尝试在 Kotlin 中使用 OKHttp 解析 JSON 字符串,但它给了我以下错误并且应用程序正在崩溃:

2019-09-30 15:27:24.871 4808-4933/com.kabelash.kotlinrepo E/AndroidRuntime:致命异常:OkHttp Dispatcher 进程:com.kabelash.kotlinrepo,PID:4808 com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 2 列路径 $ BEGIN_ARRAY 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)

我的 MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        recyclerView_main.layoutManager = LinearLayoutManager(this);

        fetchJson()

    }

    fun fetchJson() {
        val url = "https://api.myurl.com/"

        val request = Request.Builder().url(url).build()
        val client = OkHttpClient()
        client.newCall(request).enqueue(object: Callback{
            override fun onResponse(call: Call, response: Response) {
                val body = response.body?.string()
                println(body)

                val gson = GsonBuilder().create()

                val feed = gson.fromJson(body, Feed::class.java)

                runOnUiThread {
                    recyclerView_main.adapter = MainAdapter(feed)
                }
            }

            override fun onFailure(call: Call, e: IOException) {
                println("Request Failed")
            }
        })
    }
}

class Feed (val name: String, val created_at: String, val owner: Owner)

class Owner (val login: String, val avatar_url: String)

我的 MainAdapter.kt

class MainAdapter(val feed: Feed): RecyclerView.Adapter<CustomViewHolder>(){

    override fun getItemCount(): Int {
        return feed.name.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val rowCell = layoutInflater.inflate(R.layout.repo_row, parent, false)
        return CustomViewHolder(rowCell)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val fd = feed.name.get(position)
        holder.view.titleText.text = fd.toString()
    }

}

class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {
}

我花了这么多时间,但我还是想不通。我该如何解决?有什么建议吗?

【问题讨论】:

  • 似乎您的 api 返回了一个 json 对象数组,并且您试图将其转换为 Feed 对象。我说的对吗?
  • 没错..我不明白哪里出了问题或如何解决它。

标签: java android json kotlin okhttp


【解决方案1】:

API 返回一个 Json 对象数组。因此需要将其解析为数组。

你应该用val feed = gson.fromJson(body, Array&lt;Feed&gt;::class.java)代替val feed = gson.fromJson(body, Feed::class.java)

主适配器

class MainAdapter(val feed: Array<Feed>): RecyclerView.Adapter<CustomViewHolder>(){

    override fun getItemCount(): Int {
        return feed.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val rowCell = layoutInflater.inflate(R.layout.repo_row, parent, false)
        return CustomViewHolder(rowCell)
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val fd = feed.get(position)
        holder.view.titleText.text = fd.name.toString()
    }

}

class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {
}

【讨论】:

  • 如何调用主适配器中的对象? '覆盖 fun onBindViewHolder(holder: CustomViewHolder, position: Int) { //val commitTitle = commitTiles.get(position) val fd = feed.name.get(position) holder.view.titleText.text = fd.toString() } '
  • 刚刚标记它。如何在 imageview 中加载图片 url?
  • 您的问题不清楚。图片视图和图片网址在哪里?
  • 抱歉,我得到的图片 url 是这样的:'feed[position].owner.avatar_url' 现在我想将它设置为 'holder.view.proImg'。我怎样才能实现它?
  • 你需要使用像 Glider、Picasso 这样的库。但我通常使用nostra13
【解决方案2】:

您的问题在于:val feed = gson.fromJson(body, Feed::class.java)。这需要 json 并尝试反序列化到您提供的类中。但是,Feed 是一个对象,而您的 json 是一个数组的形式,因此当 gson 反序列化时,它会在开始时期待一个 {。相反,它看到的是[

有几个选项可以解决这个问题:

  • 如果您有权访问 json 的源,请修改以适合 Feed 对象。
  • Feed 更改为扩展List&lt;T&gt;Array&lt;T&gt;
  • 将列表/数组类型传递给 gson。

【讨论】:

  • 如何将其设置为 onBindViewHolder 中的文本视图?
猜你喜欢
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多