Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an array as request parameter with Retrofit 2

Tags:

I'm looking for way to add an int array (e.g [0,1,3,5]) as parameter in a GET request with retrofit 2. Then, the generated url should be like this : http://server/service?array=[0,1,3,5]

How to do this ?

like image 787
François Legrand Avatar asked Jan 11 '17 12:01

François Legrand


People also ask

How do you send parameters in retrofit?

You can pass parameter by @QueryMap Retrofit uses annotations to translate defined keys and values into appropriate format. Using the @Query("key") String value annotation will add a query parameter with name key and the respective string value to the request url .

How do you send an object in retrofit?

You can send it with help of @FormUrlEncoded for example : @FormUrlEncoded @Headers("Content-Type: application/json") @POST("getclass/") Call<ExampleClass> getExampleClass(@Field("id") int id, @Field("name") String name);


Video Answer


1 Answers

Just add it as a query param

@GET("http://server/service") Observable<Void> getSomething(@Query("array") List<Integer> array); 

You can also use int[], or Integer... as a last param;

like image 135
Andrej Jurkin Avatar answered Sep 16 '22 15:09

Andrej Jurkin