Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
kotlin com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$_Java_Android_Json_Kotlin_Okhttp - Fatal编程技术网

kotlin com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$

kotlin com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$,java,android,json,kotlin,okhttp,Java,Android,Json,Kotlin,Okhttp,我试图在Kotlin中使用OKHttp解析JSON字符串,但出现以下错误&应用程序正在崩溃: 2019-09-30 15:27:24.8714808-4933/com.kabelash.kotlinrepo E/AndroidRuntime:致命异常:OkHttp调度程序 进程:com.kabelash.kotlinrepo,PID:4808 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对

我试图在Kotlin中使用OKHttp解析JSON字符串,但出现以下错误&应用程序正在崩溃:

2019-09-30 15:27:24.8714808-4933/com.kabelash.kotlinrepo E/AndroidRuntime:致命异常:OkHttp调度程序 进程:com.kabelash.kotlinrepo,PID:4808 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$ 位于com.google.gson.internal.bind.ReflectTypeAdapterFactory$Adapter.read(ReflectTypeAdapterFactory.java:226)

我的主要活动.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) {
}
class MainAdapter(val提要:提要):RecyclerView.Adapter(){
重写getItemCount():Int{
返回feed.name.count()
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):CustomViewHolder{
val layoutInflater=layoutInflater.from(parent.context)
val rowCell=layoutInflater.充气(R.layout.repo_行,父项,false)
返回CustomViewHolder(行单元格)
}
覆盖BindViewHolder(holder:CustomViewHolder,位置:Int){
val fd=feed.name.get(位置)
holder.view.titleText.text=fd.toString()
}
}
类CustomViewHolder(val视图:视图):RecyclerView.ViewHolder(视图){
}

我花了这么多时间在这上面,但我还是不明白。我该怎么解决呢?有什么建议吗?

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

您应该将
val feed=gson.fromJson(body,feed::class.java)
放在
val feed=gson.fromJson(body,Array::class.java)

main适配器

class MainAdapter(val提要:数组):RecyclerView.Adapter(){
重写getItemCount():Int{
返回feed.count()
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):CustomViewHolder{
val layoutInflater=layoutInflater.from(parent.context)
val rowCell=layoutInflater.充气(R.layout.repo_行,父项,false)
返回CustomViewHolder(行单元格)
}
覆盖BindViewHolder(holder:CustomViewHolder,位置:Int){
val fd=进给。获取(位置)
holder.view.titleText.text=fd.name.toString()
}
}
类CustomViewHolder(val视图:视图):RecyclerView.ViewHolder(视图){
}

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

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

  • 如果您有权访问json的源代码,请修改以适应
    提要
    对象
  • 更改
    提要
    以扩展
    列表
    数组
  • 将列表/数组类型传递给gson

您的api似乎返回了一个json对象数组,而您正试图将其转换为提要对象。我说得对吗?没错。我不知道哪里出了问题,或者如何解决问题。我如何调用主适配器中的对象?'override fun onBindViewHolder(holder:CustomViewHolder,position:Int){//val committle=committles.get(position)val fd=feed.name.get(position)holder.view.titleText.text=fd.toString()}刚刚做了标记。如何在imageview中加载图像url?您的问题不清楚。图像视图和图像url在哪里?对不起,我得到的图像url是这样的:'feed[position].owner.avatar\u url'现在我想将其设置为'holder.view.proImg'。我如何实现它?你需要使用像Glider,Picasso这样的库。但我通常使用如何在onBindViewHolder中将其设置为textview?