我正在尝试解析我的应用程序中使用gson和okhttp的JSON字符串数据。 l使用回收视图显示数据。当我运行应用程序时,我得到了致命异常
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网址
[
{
"_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的新手。有什么想法我应该如何解决?
答案 0 :(得分:1)
首先将其转换为列表,然后可以遍历该列表。
val homedateList: List<HomeDate> = gson.fromJson(body, Array<HomeDate>::class.java).toList()