0

I need to make GET request using Retrofit and one of the parameters is an array of key-value par, or actually, an array of arrays, that looks like this:

 [ [1, 10], [50, 100] ]

I figured out how to perform this request in POSTMAN:

Key: durations[0][0] Value: 1
Key: durations[0][1] Value: 10

and so on. However, I have no idea ho to represent this for Retrofit. Thank you for help.

EDIT:

I tried to represent it as @Query("durations[]") List<List<Integer>> duration; Single entry is a List with two values, start and end. No luck.

2
  • please, first see this answer stackoverflow.com/questions/41590454/… Commented Feb 19, 2017 at 8:13
  • I know how to pass an array of primitive values and this is applied in my app. I think this is different because this turns into foo[]=val1&foo[]=val2&foo[]=val3, however array of arrays turns into durations[0][0]=200&durations[0][1]=300&durations[1][0]=300&durations[1][1]=400 and I do not know how to represent it for retrofit. First case it List<Integer>, but what this would be? Commented Feb 19, 2017 at 8:39

2 Answers 2

0

I advise you to use this method

Object get( @Query("durations[0][0]") int value1,@Query("durations[0][1]") int value2,
@Query("durations[1][0]") int value3,@Query("durations[1][1]") int value4)
Sign up to request clarification or add additional context in comments.

Comments

0

So basically this is how I solved this. I just manually built the parameter strings I need and put it into a Map along with value. And in your Retrofit interface make filed as @QueryMap:

@QueryMap Map<String, Integer> durations,

Have a look at my method that makes parameters:

public void prepareDurations(Pair<Integer, Integer> durationValues) {
    Map<String, Integer> paramsMap = new HashMap<>();
    if(durationValues!=null) {
        paramsMap.put("durations[0][0]", durationValues.first);
        paramsMap.put("durations[0][1]", durationValues.second);
        setDuration(paramsMap);
    }else {
        setDuration(new HashMap<>());
    }
}

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.