1

I have doubt in the code provided in documentation for Sending A Simple Request.

 val queue = Volley.newRequestQueue(this)
val url = "https://www.google.com"

// Request a string response from the provided URL.
val stringRequest = StringRequest(Request.Method.GET, url,
        Response.Listener<String> { response ->
            // Display the first 500 characters of the response string.
            textView.text = "Response is: ${response.substring(0, 500)}"
        },
        Response.ErrorListener { textView.text = "That didn't work!" })

// Add the request to the RequestQueue.
queue.add(stringRequest)

In this code first A Request is being made to the url and then the stringRequest is getting stored in the Queue. My doubt is that on making the request we will get a response, then what is the need of storing it the Queue and then rerunning all the operations like Checking in Cache Memory etc.

codebod
  • 686
  • 6
  • 17
yash
  • 35
  • 1
  • 6

1 Answers1

0

With the following statement you tell the runtime what kind of request you want to make.

val stringRequest = StringRequest(Request.Method.GET, url,
    Response.Listener<String> { response ->
        // Display the first 500 characters of the response string.
        textView.text = "Response is: ${response.substring(0, 500)}"
    },
    Response.ErrorListener { textView.text = "That didn't work!" })

The request will not be executed immediately. In order to have it executed, it has to be added to the request queue.

In other words: if you remove the following line

queue.add(stringRequest)

the request will not be made at all.

These lines from the guide on Volley summarize how it works (emphasis mine)

To send a request, you simply construct one and add it to the RequestQueue with add(), as shown above. Once you add the request it moves through the pipeline, gets serviced, and has its raw response parsed and delivered.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61