gpt4 book ai didi

kotlin - 预期 BEGIN_OBJECT 但BEGIN_ARRAY kotlin

转载 作者:行者123 更新时间:2023-12-02 12:36:02 24 4
gpt4 key购买 nike

我正在尝试解析在我的应用程序中使用 gson 和 okhttp 的 JSON 字符串数据。 l 使用回收 View 来显示数据。当我运行应用程序时,我得到了致命的异常(exception)

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.iraqairoirt.iraqairports, PID: 20692
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)
at com.iraqairoirt.iraqairports.NotamOrbi$fetchjson$1.onResponse(NotamOrbi.kt:55)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:206)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter

我的主要事件
class NotamOrbi : AppCompatActivity() {

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

val recyclerView = findViewById<RecyclerView>(R.id.recyclerDate)
recyclerView.layoutManager = LinearLayoutManager(this)

fetchjson()

}

fun fetchjson() {

val url =
"/&locations=orbi"
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 homedata = gson.fromJson(body, HomeDate::class.java)

runOnUiThread{
recyclerDate.adapter=NotamOrbiAdapter(homedata)

}

}

override fun onFailure(call: Call, e: IOException) {

}


})


}

class HomeDate( val datanotam:ArrayList<NotamORBI>)
class NotamORBI(val id : String)

class NotamOrbiAdapter (val datajson:HomeDate): RecyclerView.Adapter<NotamOrbiAdapter.ViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {

val v = LayoutInflater.from(p0.context).inflate(R.layout.notam_reclycer_card, p0, false)
return ViewHolder(v)
}

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

override fun onBindViewHolder(p0: ViewHolder, p1: Int) {

val data = datajson.datanotam.get(p1)
p0?.itemView.id_notam.text=data.id

}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

val id_notam = itemView.findViewById<TextView>(R.id.id_notam)
}
}
}

问题出在第 55 行 val homedata = gson.fromJson(body, HomeDate::class.java)
数据json url
 [
{
"_id": "5c0b4ed2ab233e849941925c",
"id": "A0466/18",
"entity": "MR",
"status": "LC",
"Qcode": "MRLC",
"Area": "AGA",
"SubArea": "Movement and landing area",
"Condition": "Limitations",
"Subject": "Runway",
"Modifier": "Closed",
"message": "RWY 15L/33R CLSD DUE TO MAINT DURING VMC ONLY.\nCREATED: 05 Dec 2018 06:59:00 \nSOURCE: ORBIYNYX",
"startdate": "2018-12-07T11:00:00.000Z",
"enddate": "2018-12-17T13:00:00.000Z",
"all": "A0466/18 NOTAMN\nQ) ORBB/QMRLC/IV/NBO/A/000/999/3316N04414E005\nA) ORBI\nB) 1812071100\nC) 1812171300\nD) MON FRI 1100-1300\nE) RWY 15L/33R CLSD DUE TO MAINT DURING VMC ONLY.\nCREATED: 05 Dec 2018 06:59:00 \nSOURCE: ORBIYNYX",
"location": "ORBI",
"isICAO": true,
"Created": "2018-12-05T06:59:00.000Z",
"key": "A0466/18-ORBI",
"type": "airport",
"StateCode": "IRQ",
"StateName": "Iraq"
}
]

我知道问题出在 json 数组上,但我不知道如何修复我是 gson 的新手。任何想法我应该如何解决它?

最佳答案

首先将其转换为列表,然后您可以遍历该列表。

val homedateList: List<HomeDate> = gson.fromJson(body, Array<HomeDate>::class.java).toList()

关于kotlin - 预期 BEGIN_OBJECT 但BEGIN_ARRAY kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53685627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com