Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit optional and required fields

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?

like image 812
Orbit Avatar asked May 03 '16 23:05

Orbit


People also ask

How do you add parameters to retrofit request?

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.

How do you add a query option to a REST web service call in retrofit?

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.

What is query annotation in 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.


1 Answers

@FieldMap and @Query params both support optional fields. As you mentioned, simply pass null if you don't want to pass a value.

like image 174
Ryan Avatar answered Sep 21 '22 06:09

Ryan