Retrofit query parameters

What are the ways in which we can modify query parameters in Retrofit

For a more complete and in-depth explanation, check out our complete Retrofit course

Download the starter project here

Github repository

Here is an example of a couple of query parameters what were added to a URL

URL = “https://example.com/getUser?name=Alex&age=24”

Normally query parameters are added by putting a question mark after the endpoint and placing the parameters in the form key=value, separated by an & sign.

So how can we achieve this in Retrofit? We have multiple options

Static parameters

The parameters can be added to the endpoint in a static way, so there’s no way to modify and change them.

If you know that you have a certain parameter that needs to be there all the time or even multiple parameters, then we can use this format.

Let’s implement this functionality in our project. Add the following code to the ApiCall interface

@GET(“apiCall?name=Alex&age=24”)

fun callQueryStatic(): Call<ApiCallResponse>

Change the ApiCallService class, update the call function to look like this

fun call(context: Context) = getApi(context).callQueryStatic()

Running the code will give us an expected output

query_static

Dynamic parameters

We can specify parameters at run time, and we do this with the @Query annotation. In the annotation we need to provide the key for that parameter, then we have the variable name and type. We can chain as many parameters as needed.

Update the ApiCall interface

@GET(“apiCall”)

fun callQueryDynamic(@Query(“name”) n: String, @Query(“age”) a: Int): Call<ApiCallResponse>

Update the ApiCallService class, call function

fun call() = api.callQueryDynamic(“James”, 27)

Running the code will give us

query_dnamic

Optional parameters

It’s the same construct behind dynamic parameters, however these are marked as nullable. So when we mark a parameter as nullable and we pass a null value, that particular parameter will not be passed to the URL request.

Let’s try that out in our code. Update the ApiCall interface, callQueryDynamic method. Make the parameters optional (nullable)

@GET(“apiCall”)

fun callQueryDynamic(@Query(“name”) n: String?, @Query(“age”) a: Int?): Call<ApiCallResponse>

Update the ApiCallService class, call function

fun call() = api.callQueryDynamic(null, 27)

query_optional

Multiple parameters

Finally, the last option is to pass multiple parameters to our request. We can pass as many parameters as we want, with the annotation @QueryMap that takes a map object. The map key corresponds to the parameter key, the value of the map corresponds to the value of the parameter.

Update the ApiCall interface to add multiple parameters

@GET(“apiCall”)

fun callQueryMultiple(@QueryMap params: Map<String, String>): Call<ApiCallResponse>

Update the ApiCallService class to invoke this new function

fun call() = api.callQueryMultiple(hashMapOf(Pair(“a”, “foo”), Pair(“b”, “bar”)))

query_multiple

For a more complete and in-depth explanation, check out our complete Retrofit course

Close Bitnami banner
Bitnami