I want to get the data from an API Call and I am trying it with Volley and a StringRequest in Android Studio. I want to get the "nachricht" out of there. The JSON Object of the Response from the URL looks like this :
{
"tmp": {
"msg": [
{
"nachricht": "Hallo1"
},
{
"nachricht": "Hallo"
}
]
}
}
And my API Call from Android Studio looks like this:
private fun getDataAPI() {
val url = "http://theurl.com" //Your URL in this field
var textView = findViewById<TextView>(R.id.timestamp1)
textView.movementMethod = ScrollingMovementMethod()
var textView2 = findViewById<TextView>(R.id.timestamp2)
textView2.movementMethod = ScrollingMovementMethod()
val queue = Volley.newRequestQueue(this)
val stringReq = StringRequest(Request.Method.GET, url,
{ response ->
var strResp = response.toString()
val jsonObj: JSONObject = JSONObject(strResp)
val jsonTmp = jsonObj.getJSONObject("tmp")
val jsonArray: JSONArray = jsonTmp.getJSONArray("msg")
var struser: String = ""
for (i in 0 until jsonArray.length()) {
var jsonInner: JSONObject = jsonArray.getJSONObject(i)
struser = "\n" + jsonInner.get("nachricht")
Log.i("str_user", struser)
textView!!.text = struser
}
},
{textView!!.text = "That didn't work!" })
queue.add(stringReq)
}
Why does it always say "That didn't work". How can I get "nachricht". Is the access to the JSON Objects wrong? Or should I use a JSONRequest from Volley and if this is the problem how do I have to use a JSONRequest from Volley.