skip to Main Content

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


  1. 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:

    val jsonObject = JSONObject(response) // Parse the response as a JSONObject
    
    // Get the title
    val responseName = jsonObject.getString("Title")
    
    // Get the responseURL array
    val responseUrlArray = jsonObject.getJSONArray("responseURL")
    
    // Iterate through the array to extract each URL
    val urlList = mutableListOf<String>()
    for (i in 0 until responseUrlArray.length()) {
        val url = responseUrlArray.getString(i) // Extract each URL as a String
        urlList.add(url)
    }
    
    // Now urlList contains all the URLs
    for (url in urlList) {
        println(url) // Print or use each URL
    }
    

    Explanation

    JSONObject Parsing: Convert the entire response into a JSONObject.

    val jsonObject = JSONObject(response)
    

    Extract responseURL as a JSONArray:

    val responseUrlArray = jsonObject.getJSONArray("responseURL")
    

    Iterate through the JSONArray:

    1. Use a loop to extract each URL as a String.
    2. Add each URL to a list (urlList) for further processing.

    It should solve the issue.
    Thanks!

    Login or Signup to reply.
  2. 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:


    // Parse the title and requestedURL array from the response
    val responseName = response.getString("Title") // Title is a string
    
    // Get the JSON array for "responseURL"
    val responseUrlsArray = response.getJSONArray("responseURL")
    
    // Convert the JSON array to a Kotlin list of strings
    val responseUrls = mutableListOf<String>()
    for (i in 0 until responseUrlsArray.length()) {
        responseUrls.add(responseUrlsArray.getString(i))
    }
    
    // Now responseUrls contains all the URLs as a List<String>
    responseUrls.forEach { url ->
        println("URL: $url")
    }
    

    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:


    [
        "https://i.mg.xxx/p372qxd3694e1.jpg",
        "https://i.mg.xxx/qs06k8e3694e1.jpg",
        "https://i.mg.xxx/mjfzw6e3694e1.jpg"
    ]
    

    You can then process these URLs as needed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search