When using Retrofit, I know you can use @FieldMap Map<String, String> options
to specify optional queries.
Say that I have a api call that had 2 required fields and 3 optional fields.
How would I format these calls?
Would it be something like
Call<Response> getStuff(@Query("user_id") String userId, @Query("password") String password, @FieldMap Map<String, String> options)
or would the entire thing be a single @FieldMap like:
Call<Response> getStuff(@FieldMap Map<String, String> options)
and with this option would you just fill in the required fields and then use null
for the optionals?
Adding query parameters to single requests is straight forward. You're using the @Query annotation for your parameter declaration within the interface methods. This tells Retrofit to translate the provided query parameter name and value to the request and append the fields to the url.
Append the query to the end of the request URL. Add a parameter for the query to the function that makes the request and annotate the parameter with @Query. Use the Query class to build a request. Use the addQuery() method in the Retrofit builder.
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.
@FieldMap
and @Query
params both support optional fields. As you mentioned, simply pass null
if you don't want to pass a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With