kotlin com.google.gson.JsonSyntaxException:

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


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


我的 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)



我在其中花了很多时间,但仍然无法弄清楚。我该如何解决呢?有什么建议么?


慕斯709654
浏览 77回答 2
2回答

哆啦的时光机

API 返回一个 Json 对象数组。因此,您需要将其解析为数组。而不是val feed = gson.fromJson(body, Feed::class.java)你应该放val feed = gson.fromJson(body, Array<Feed>::class.java)主适配器class MainAdapter(val feed: Array<Feed>): RecyclerView.Adapter<CustomViewHolder>(){&nbsp; &nbsp; override fun getItemCount(): Int {&nbsp; &nbsp; &nbsp; &nbsp; return feed.count()&nbsp; &nbsp; }&nbsp; &nbsp; override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {&nbsp; &nbsp; &nbsp; &nbsp; val layoutInflater = LayoutInflater.from(parent.context)&nbsp; &nbsp; &nbsp; &nbsp; val rowCell = layoutInflater.inflate(R.layout.repo_row, parent, false)&nbsp; &nbsp; &nbsp; &nbsp; return CustomViewHolder(rowCell)&nbsp; &nbsp; }&nbsp; &nbsp; override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {&nbsp; &nbsp; &nbsp; &nbsp; val fd = feed.get(position)&nbsp; &nbsp; &nbsp; &nbsp; holder.view.titleText.text = fd.name.toString()&nbsp; &nbsp; }}class CustomViewHolder(val view: View): RecyclerView.ViewHolder(view) {}

繁星coding

您的问题在于该行:val feed = gson.fromJson(body, Feed::class.java)。这将获取 json 并尝试反序列化为您提供的类。但是,Feed是一个对象,并且您的 json 是数组的形式,因此当 gson 反序列化时,它{在开始时需要 a 。相反,它看到的是[.有几个选项可以解决这个问题:如果您有权访问 json 的源,请修改以适合对象Feed。更改Feed为扩展List<T>或Array<T>.将列表/数组类型传递给 gson。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java