我想在我的应用程序中获取json数据。 l使用回收视图显示数据。当我运行应用程序时,我得到了FATAL EXCEPTION
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.iraqairoirt.iraqairports, PID: 29757
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
E/AndroidRuntime: Caused by: org.json.JSONException: Value [{"_id":"5c0ba2a7ab233e8499463a95","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"},{"_id":"5c0ba2a7ab233e8499463a96","id":"A0409\/18","entity":"MX","status":"LC","Qcode":"MXLC","Area":"AGA","SubArea":"Movement and landing area","Condition":"Limitations","Subject":"Taxiway(s)","Modifier":"Closed","message":"TWY M-NORTH CLSD FOR MAINT BETWEEN SPOT 3 AND TWY M6. UAVS CAN\nOPERATE AT THEIR OWN DISCRETION.\n)\nUNITED STATES NOF DISCLAIMS FOREIGN NOTAM ACCURACY OR COMPLETENESS\nCREATED: 01 Dec 2018 11:44:00 \nSOURCE: LIIAYNYX","startdate":"2018-11-23T04:00:00.000Z","enddate":"2018-12-21T16:00:00.000Z","all":"A0409\/18 NOTAMN\nQ) ORBB\/QMXLC\/IV\/M\/A\/000\/999\/3316N04414E005\nA) ORBI B) 1811230400 C) 1812211600\nD) FRI 0400-1600\nE) TWY M-NORTH CLSD FOR MAINT BETWEEN SPOT 3 AND TWY M6. UAVS CAN\nOPERATE AT THEIR OWN DISCRETION.\n)\nUNITED STATES NOF DISCLAIMS FOREIGN NOTAM ACCURACY OR COMPLETENESS\nCREATED: 01 Dec 2018 11:44:00 \nSOURCE: LIIAYNYX","location":"ORBI","isICAO":true,"Created":"2018-12-01T11:44:00.000Z","key":"A0409\/18-ORBI","type":"airport","StateCode":"IRQ","StateName":"Iraq"},{"_id":"5c0ba2a7ab233e8499463a97","id":"A0386\/18","entity":"FA","status":"TT","Qcode":"FATT","Area":"AGA","SubArea":"Facilities and services","Condition":"Other","Subject":"Aerodrome","Modifier":"AIRAC AIP Amendments","message":"TRIGGER NOTAM - PERM AIRAC AIP AMDT 07\/18 WEF 06 DEC 2018.\nOMNIDIRECTIONAL DEPARTURE CHART UPDATED.\n)\nUNITED STATES NOF DISCLAIMS FOREIGN NOTAM ACCURACY OR COMPLETENESS\nCREATED: 01 Dec 2018 11:44:00 \nSOURCE: LIIAYNYX","startdate":"2018-12-06T00:00:00.000Z","enddate":"2018-12-19T23:59:00.000Z","all":"A0386\/18 NOTAMN\nQ) ORBB\/QFATT\/IV\/BO\/A\/000\/999\/3316N04414E005\nA) ORBI B) 1812060000 C) 1812192359\nE) TRIGGER NOTAM - PERM AIRAC AIP AMDT 07\/18 WEF 06 DEC 2018.\nOMNIDIRECTIONAL DEPARTURE CHART UPDATED.\n)\nUNITED STATES NOF DISCLAIMS FOREIGN NOTAM ACCURACY OR COMPLETENESS\nCREATED: 01 Dec 2018 11:44:00 \nSOURCE: LIIAYNYX","location":"ORBI","isICAO":true,"Created":"2018-12-01T11:44:00.000Z","key":"A0386\/18-ORBI","type":"airport","StateCode":"IRQ","StateName":"Iraq"},{"_id":"5c0ba2a7ab233e8499463a98","id":"A0445\/18","entity":"MT","status":"CM","Qcode":"MTCM","Area":"AGA","SubArea":"Movement and landing area","Condition":"Changes","Subject":"Threshold","Modifier":"Displaced","message":"THR RWY 33L DISPLACED BY 400M. RWY 15R\/33L DECLARED DISTANCES\nCHANGED AS FOLLOWS: TORA, TODA, ASDA, LDA = 2901M. THE FIRST 400M \nOF\nRWY 33L IS NOT AVAILABLE FOR TAXYING ACFT. ALL ACFT DEPARTING FROM\nRWY 33L CAN EXPECT TO ENTER VIA TWY P2 TO P6 OR M6 AND BACKTRACK \nFOR\nDEPARTURE. ALL ACFT ARRIVING ON RWY 15R CAN EXPECT TO BACKTRACK \nAFTER\nLANDING AND VACATE VIA TWY P2 TO P6 OR M6. RWY 15R\/33L INSTRUMENT\nAPPROACHES SUSPENDED. ONLY VISUAL APPROACHES AVAILABLE. CIVILIAN\nAIRCRAFT MAY ONLY USE RWY 15R\/33L DURING DAYLIGHT HOURS AND VMC.\nCREATED: 29 Nov 2018 10:54:00 \nSOURCE: ORBIYNYX","startdate":"2018-11-29T11:00:00.000Z","enddate":"2019-02-28T10:59:00.000Z","all":"A0445\/18 NOTAMN\nQ) ORBB\/QMTCM\/IV\/NBO\/A\/000\/999\/3316N04414E005\nA) ORBI\nB) 1811291100\nC) 1902281059\nE) THR RWY 33L DISPLACED BY 400M. RWY 15R\/33L DECLARED DISTANCES\nCHANGED AS FOLLOWS: TORA, TODA, ASDA, LDA = 2901M. THE FIRST 400M \nO
F\nRWY 33L IS NOT AVAILABLE FOR TAXYING ACFT. ALL ACFT DEPARTING FROM\nRWY 33L CAN EXPECT TO ENTER VIA TWY P2 TO P6 OR M6 AND BACKTRACK \nFOR\nDEPARTURE. ALL ACFT ARRIVING ON RWY 15R CAN EXPECT TO BACKTRACK \nAFTER\nLANDING AND VACATE VIA TWY P2 TO P6 OR M6. RWY 15R\/33L INSTRUMENT\nAPPROACHES SUSPENDED. ONLY VISUAL APPROACHES AVAILABLE. CIVILIAN\nAIRCRAFT MAY ONLY USE RWY 15R\/33L DURING DAYLIGHT HOURS AND VMC.\nCREATED: 29 Nov 2018 10:54:00 \nSOURCE: ORBIYNYX","location":"ORBI","isICAO":true,"Created":"2018-11-29T10:54:00.000Z","key":"A0445\/18-ORBI","type":"airport","StateCode":"IRQ","StateName":"Iraq"}] of type org.json.JSONArray cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONObject.<init>(JSONObject.java:160)
at org.json.JSONObject.<init>(JSONObject.java:173)
at com.iraqairoirt.iraqairports.NotamOrbi$Arr.handleJson(NotamOrbi.kt:90)
at com.iraqairoirt.iraqairports.NotamOrbi$Arr.onPostExecute(NotamOrbi.kt:77)
at com.iraqairoirt.iraqairports.NotamOrbi$Arr.onPostExecute(NotamOrbi.kt:45)
at android.os.AsyncTask.finish(AsyncTask.java:660)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
主要活动使用回收视图保存所有数据
class NotamOrbi : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.notam_orbi)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerDate)
val data = ArrayList<NOTAMORBI>()
val adapter = NotamOrbiAdapter(data)
recyclerView.adapter = adapter
val url =
"/locations=orbi"
Arr().execute(url)
}
inner class Arr : AsyncTask<String, String, String>() {
override fun onPreExecute() {
super.onPreExecute()
}
// for build connection
override fun doInBackground(vararg url: String?): String {
var text: String
val connection = URL(url[0]).openConnection() as HttpURLConnection
connection.connectTimeout = 700
try {
connection.connect()
text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } }
} finally {
connection.disconnect()
}
return text
}
override fun onPostExecute(result: String?) {
super.onPostExecute(result)
handleJson(result)
}
override fun onProgressUpdate(vararg text: String?) {
}
@SuppressLint("WrongViewCast")
private fun handleJson(jsonString: String?) {
val jsonObj = JSONObject(jsonString)
// val result = jsonObj.getJSONObject("result")
// val response = result.getJSONObject("response")
// val jsonArray = JSONArray(response.get("data").toString())
val list = ArrayList<NOTAMORBI>()
var x = 0
while (x < jsonObj.length()) {
val jsonObject = jsonObj.getJSONObject(x.toString())
list.add(NOTAMORBI(
jsonObject.getString("id")
)
)
x++
}
list.forEach(::println)
}
}
class NotamOrbiAdapter(val list: ArrayList<NOTAMORBI>) : 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 list.size
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
val data: NOTAMORBI = list[p1]
p0.id_notam.text = data.idNotam
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val id_notam = itemView.findViewById<TextView>(R.id.id_notam)
}
}
}
数据类
data class NOTAMORBI (val idNotam : String)
数据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"
}
]
请问有人有解决办法吗?
答案 0 :(得分:0)
您的json是一个数组,它必须是一个对象;删除[
和]
包装您的json
或者使用JSONArray
代替JSONObject