为什么我无法获取我的JSON文件的值?

您好,我正尝试在Android Studio中使用Kotlin提取我的JSON文件的数据:

这是我的JSON文件:

val future = RequestFuture.newFuture < JSONObject > () val request = JsonObjectRequest("localhost", JSONObject(), future, future) val requestQueue = Volley.newRequestQueue(this) requestQueue.add(request) try { val array = JSONObject().getJSONArray("server_response") for (i in 0..array.length() - 1) { val objectYear = array.getJSONObject(i) val year = objectYear.getString("birthday") val response = future.get() // this will block textView2.setText(year.toString) // It does not work } } catch (e: JSONException) { e.printStackTrace() } 

我想在textView2中显示值年,谢谢你的帮助!

尝试这个

 var jsonData = JSONObject() val future = RequestFuture.newFuture < JSONObject > () var listener = Response.Listener<JSONObject>() { fun onResponse(response: JSONObject) { try { val array = response.getJSONArray("server_response") for (i in 0..array.length() - 1) { val objectYear = array.getJSONObject(i) val year = objectYear.getString("birthday") val response = future.get() // this will block textView2.setText(year.toString) // It does not work } } catch (e: JSONException) { e.printStackTrace() } } } val request = JsonObjectRequest("localhost",jsonData , listener , future) val requestQueue = Volley.newRequestQueue(this) requestQueue.add(request) requestQueue.start()