I’ve been working on this for some time and searching the internet, but I just can’t find the solution.
I use volley to query an api and receive this response.
{
"Title": "Groupname",
"requestedURL": "url",
"responseURL": [
"https://i.mg.xxx/p372qxd3694e1.jpg",
"https://i.mg.xxx/qs06k8e3694e1.jpg",
"https://i.mg.xxx/mjfzw6e3694e1.jpg"
]
}
Title and requestedUrl are no problem
val responseName = response.getString("Title")
But I can’t get the data from requestedURL.
I have tried various things with .getJSONArray / .getJSONObject – what I found on the internet, but nothing worked, maybe because I didn’t understand it properly
2
Answers
The issue lies in extracting data from the responseURL field, which is a JSON array. To handle this correctly, follow these steps:
Solution
Here’s how you can extract responseURL from the JSON response using Volley:
Explanation
JSONObject Parsing: Convert the entire response into a JSONObject.
Extract responseURL as a JSONArray:
Iterate through the JSONArray:
It should solve the issue.
Thanks!
The issue likely arises because requestedURL in the JSON you provided is not an individual string or an object but an array of strings. To handle this properly with Volley, you need to use getJSONArray() and then iterate over the array to extract the strings.
Here’s how you can do it:
Explanation:
getString("Title"): Directly gets the Title from the JSON as it is a simple string.
getJSONArray("responseURL"): Retrieves the responseURL field as a JSON array.
Iterate through the array using a for loop and extract each string using getString(index).
Store the extracted strings in a Kotlin List for easy handling.
Result:
For your example, responseUrls will contain:
You can then process these URLs as needed.