0

I'm trying to pass multiple Values to a SINGLE parameter for example :

http://api.giphy.com/v1/gifs?api_key=dc6zaTOxFJmzC&ids=feqkVgjJpYtjy,7rzbxdu0ZEXLy

I tried the following :

 @GET("gifs")
    Call<GIFModelMain> getGifsByID(@Field("ids")ArrayList<String> values, @Query("api_key") String API_KEY);

In my activity :

ArrayList<String> x = new ArrayList<>();
        x.add("feqkVgjJpYtjy");
        x.add("7rzbxdu0ZEXLy");
        gifCall = interf.getGifsByID(x, BuildConfig.GIPHY_API_TOKEN);

But the built URL is of form:

http://api.giphy.com/v1/gifsids=feqkVgjJpYtjy&ids=7rzbxdu0ZEXLy&api_key=API_KEY_BLANK

I looked up similar questions but found no correct answer.

EDIT: As per what TooManyEduardos said i changed my Interface to

@GET("gifs")
    Call<GIFModelMain> getGifsByID(@QueryMap Map<String, String> parameters,@Query("api_key") String API_KEY);

And my activity is now :

Map<String,String> map = new HashMap<>();
        map.put("ids","feqkVgjJpYtjy");
        map.put("ids","7rzbxdu0ZEXLy");

        gifCall = interf.getGifsByID(map, BuildConfig.GIPHY_API_TOKEN);

But the built URL is still : 03-30 02:46:23.922: E/FavActivity(21607): Url : api.giphy.com/v1/gifs?ids=7rzbxdu0ZEXLy&api_key=KEY_HERE

Aldrich M
  • 5
  • 4

1 Answers1

0

You're looking for a

Map<String,String>

And in your @Get interface, you'll receive it like this:

(@QueryMap Map<String, String> parameters)

So your whole interface call would be like this:

@GET("gifs")
Call<GIFModelMain> getGifsByID(@QueryMap Map<String, String> parameters);

I wrote a whole tutorial on how to use Retrofit 2 if you want to check it out: http://toomanytutorials.blogspot.com/2016/03/network-calls-using-retrofit-20.html

EDIT If you really want to pass multiple parameters, regardless of their key name, you can always do this:

Call<GIFModelMain> getGifsByID(@Query("api_key") String API_KEY, @Query("ids") String id1, @Query("ids") String id2, @Query("ids") String id3);

The obvious problem here is that you'll have to make multiple versions of the same method depending on how many ids you're passing

TooManyEduardos
  • 4,206
  • 7
  • 35
  • 66
  • Following my advice, you'll have this: Call getGifsByID(@QueryMap Map parameters,@Field("ids")ArrayList values, @Query("api_key") String API_KEY); – TooManyEduardos Mar 29 '16 at 21:19
  • Change it to this: Call getGifsByID(@QueryMap Map parameters); – TooManyEduardos Mar 29 '16 at 21:19
  • so now you forget the arraylist and you also pass the API_KEY as another key-value pair for the Map parameter – TooManyEduardos Mar 29 '16 at 21:20
  • I put the "ids" parameter in my first String in the Map. Will i still need @Field("ids")ArrayList values ? – Aldrich M Mar 29 '16 at 21:21
  • Nope. Just pass a Map with all your params together since they're all parameters of the GET call – TooManyEduardos Mar 29 '16 at 21:22
  • Damn just realised that Map doesn't allow duplicated keys. And since i need multiple values for ids it wont allow that. – Aldrich M Mar 29 '16 at 21:25
  • I updated my answer. Let me know if you like that approach. – TooManyEduardos Mar 29 '16 at 21:28
  • The problem is ids will take any Y number of values, and the Giphy API doesnt allow me to do this: http://api.giphy.com/v1/gifs?api_key=dc6zaTOxFJmzC&ids=feqkVgjJpYtjy&ids=7rzbxdu0ZEXLy – Aldrich M Mar 29 '16 at 21:31
  • I found an alternate way to do this : https://stackoverflow.com/questions/21617792/can-i-use-varargs-in-a-retrofit-method-declaration – Aldrich M Mar 29 '16 at 21:35
  • Yeah you can look into that. I also found something called MultiHashMap, but I've never used it. I wonder if giphy allows something like ids1, ids2, ids3 as aliases. Well, at least you're on the right path now – TooManyEduardos Mar 29 '16 at 21:36