This post belongs to our Retrofit series and describes how to perform requests with single and multiple query parameters of same name. The following overview illustrates the posts from mentioned series.
Retrofit Series Overview
- Getting Started and Creating an Android Client
- Basics of API Description
- Creating a Sustainable Android Client
- URL Handling, Resolution and Parsing
- How to Change API Base Url at Runtime
- Multiple Server Environments (Develop, Staging, Production)
- Share OkHttp Client and Converters between Retrofit Instances
- Upgrade Guide from 1.9
- Beyond Android: Retrofit for Java Projects
- How to use OkHttp 3 with Retrofit 1
- Synchronous and Asynchronous Requests
- Send Objects in Request Body
- Add Custom Request Header
- Manage Request Headers in OkHttp Interceptor
- Dynamic Request Headers with @HeaderMap
- Multiple Query Parameters of Same Name
- Optional Query Parameters
- Send Data Form-Urlencoded
- Send Data Form-Urlencoded Using FieldMap
- How to Add Query Parameters to Every Request
- Add Multiple Query Parameter With QueryMap
- How to Use Dynamic Urls for Requests
- Constant, Default and Logic Values for POST and PUT Requests
- Cancel Requests
- Reuse and Analyze Requests
- Optional Path Parameters
- How to Send Plain Text Request Body
- Customize Network Timeouts
- How to Trust Unsafe SSL certificates (Self-signed, Expired)
- Dynamic Endpoint-Dependent Interceptor Actions
- How to Update Objects on the Server (PUT vs. PATCH)
- How to Delete Objects on the Server
- Introduction to (Multiple) Converters
- Adding & Customizing the Gson Converter
- Implementing Custom Converters
- How to Integrate XML Converter
- Access Mapped Objects and Raw Response Payload
- Supporting JSON and XML Responses Concurrently
- Handling of Empty Server Responses with Custom Converter
- Send JSON Requests and Receive XML Responses (or vice versa)
- Unwrapping Envelope Responses with Custom Converter
- Wrapping Requests in Envelope with Custom Converter
- Define a Custom Response Converter
Query Parameters
Query parameters are a common way to pass data from clients to servers. We all know them for requests. Let's face the example below which requests a specific task with id=123
from our example API.
https://api.example.com/tasks?id=123
The response from example API is only a single task with id=123
.
The Retrofit method definition for query parameters is straight forward.
Retrofit 2
public interface TaskService {
@GET("/tasks")
Call<Task> getTask(@Query("id") long taskId);
}
Retrofit 1.9
public interface TaskService {
@GET("/tasks")
Task getTask(@Query("id") long taskId);
}
The method getTask(…)
requires the parameter taskId
. This parameter will be mapped by Retrofit to given @Query()
parameter name. In this case the parameter name is id
which will result in a final request url part like
/tasks?id=<taskId>
Multiple Query Parameters
Some use cases require to pass multiple query parameters of the same name. Regarding the previous example of requesting tasks from an API, we can extend the query parameter to accept a list with multiple task ids.
The request url we want to create should look like
https://api.example.com/tasks?id=123&id=124&id=125
The expected server response should be a list of tasks with the given ids=[123, 124, 125]
from url query parameters.
The Retrofit method to perform requests with multiple query parameters of the same name is done by providing a list of ids
as a parameter. Retrofit concatenates the given list to multiple query parameters of same name.
Retrofit 2
public interface TaskService {
@GET("/tasks")
Call<List<Task>> getTask(@Query("id") List<Long> taskIds);
}
Retrofit 1.9
public interface TaskService {
@GET("/tasks")
List<Task> getTask(@Query("id") List<Long> taskIds);
}
The resulting request url will look like the example above in the beginning of this section (Multiple Query Parameters).
If you run into questions or problems, find us on Twitter @futurestud_io or in the comment section below.
Enjoy coding & make it rock.