Passing parameters to volley request

Updated 4 July 2023

Save

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. But as being new its quiet confusing for most of us.

One of the confusion is setting parameters in volley requests. The paramenters can be set in a volley request by overriding getParams() method of Request<T> class. But the problem is that the getParams() is called only in the case of POST or PUT request. As the documentation says:

But what in case of GET method!!

In case of GET method the paraments are concatenated with the url and then url is passed.

Its quiet easier approach but it becomes a lot of work when you have a lot of parameters then you have to concatenate each and every paramenter in the url. In GET request the getParams() is not even called so there is no way you can use the methods

In case of POST method you can override the getParams() and give all the parameters you want over there.

Also for JSONobjectRequest the getParams() doen’t even work for POST request so you have to make a customRequest and override getParams() method over there. Its because JsonObjectRequest is extended JsonRequest which override getBody() method directly, so your getParam() would never invoke. To invoke your getParams() method you first have to override getBody(). Or for a simple solution you can use StringRequest.

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • ChampRocks Patel
    I thought so. , i did with concatination its ok, but should be dynamic way like mapping with get also, actually retrifit does the same we create the interface and provide the header query (param1) and value for it. Nice tutorial, i was confised for get method. can u please provide tutorial and flow like first calledmap? If we use post method than it request for string request? If we r doing authentication how the flow works?
  • Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home