0

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.

  • Update: Now i finally know that with my API Call I never come to the response it always go straight to "That didn't work". With a other test api the call works sucessfull. Why not here? – Chrasher54 Dec 14 '21 at 21:06
  • Instead of just setting text to "that didn't work", why don't you actually handle the error you're getting in that block? Log it, show a toast, set the text on that textview. SOMETHING to know what the error actually is. – dominicoder Dec 15 '21 at 01:30
  • Did you allow insecure HTTP traffic? [Android 8: Cleartext HTTP traffic not permitted](https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted) – Alexander Hoffmann Dec 15 '21 at 10:46
  • Yeah you're right I should show the error. – Chrasher54 Dec 15 '21 at 20:39
  • I will try the HTTP traffic thank you! – Chrasher54 Dec 15 '21 at 20:40
  • HTTP Traffic was the problem! Thanks for your help! – Chrasher54 Dec 19 '21 at 16:45

0 Answers0