Start a Project

Passing parameters to volley request

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.

 

Exit mobile version