13

I have an instance where I need to pass multiple of the same named parameters to a server (array of data).

Changing it is not a possibility.

http://test.com?test[]=1&test[]=2&test[]=3

How do I accomplish this with RetroFit? I see that you can pass a map of values, but that doesn't help as the keys are all identical.

Any help would be great... really hoping there's a clean way/workaround or else I'm going to need to use another api lib and do a project refactor.

3
  • 1
    Are you using retrofit 1.4.+? It looks like arrays were added to a field, but I haven't tried this yet. * New: @Field now accepts List or arrays for multiple values. * New: @Query and @EncodedQuery now accept List or arrays for multiple values. Commented Feb 19, 2014 at 21:33
  • You are correct. Found that a few hours ago. If you make that an answer I'll mark it as the solution. Commented Feb 19, 2014 at 23:59
  • 1
    I wouldn't call it bizarre. Both html checkbox and select multiple controls will encode the form like this. Commented May 22, 2014 at 15:02

2 Answers 2

19

Retrofit as of 1.4.0 added the ability to send an array or List as a @Field or @Query parameter.

New: @Query and @EncodedQuery now accept List or arrays for multiple values.
New: @Field now accepts List or arrays for multiple values.
Sign up to request clarification or add additional context in comments.

3 Comments

The relevant lines in source can be found here: github.com/square/retrofit/pull/377/…
Any idea how a similar problem with Part parameter in Multipart can be tackled?
How do they get encoded I wonder? I will look at the pull request mentioned above.
6

I am using retrofit:1.9.0, one way of doing http://test.com?test[]=1&test[]=2&test[]=3 is like this

void test(@Query("test[]") ArrayList<String> values);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.