您好我试图在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中显示值年份,谢谢你的帮助!!!
答案 0 :(得分:0)
试试这个
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()