The previous post guided you through the options to add custom headers to your requests. Today, we’re diving into the topic on how to use Retrofit to make request parameters optional.
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 with Retrofit
Retrofit uses annotations to define query parameters for requests. The annotation is defined before method parameters and specifies the request parameter name. The desired value for this parameter is passed during method call. Let's face a concrete example. The code below illustrates a request method which has /tasks
as API endpoint url and provides the option to pass a sort
parameter for desired sorting.
Retrofit 2
public interface TaskService {
@GET("/tasks")
Call<List<Task>> getTasks(@Query("sort") String order);
}
Retrofit 1.9
public interface TaskService {
@GET("/tasks")
List<Task> getTasks(@Query("sort") String order);
}
Assuming that your base url to your API is https://your.api.com
, the request using the TaskService
from above and calling the getTasks
method results in this request url: https://your.api.com/tasks?sort=value-of-order-parameter
Optional Query Parameters
Depending on the API design, the sort
parameter might be optional. In case you don’t want to pass it with the request, just pass null
as the value for order
during method call.
service.getTasks(null);
Retrofit skips null
parameters and ignores them while assembling the request. Keep in mind, that you can’t pass null
for primitive data types like int
, float
, long
, etc. Instead, use Integer
, Float
, Long
, etc and the compiler won’t be grumpy.
The upcoming example depicts the service definition for the previously mentioned data types.
Retrofit 2
public interface TaskService {
@GET("/tasks")
Call<List<Task>> getTasks(
@Query("sort") String order,
@Query("page") Integer page);
}
Retrofit 1.9
public interface TaskService {
@GET("/tasks")
List<Task> getTasks(
@Query("sort") String order,
@Query("page") Integer page);
}
Now you can pass null
for either of the defined query parameters order
and page
when calling the getTasks
method.
service.getTasks(null, null);
Summary
In this tutorial you've seen how you can use optional query parameters.
Please let us know about any problems you ran into on Twitter @futurestud_io or in the comment section below.
Enjoy coding & make it rock!
Additional Ressources
- Retrofit API declaration: section Url Manipulation