8

We need to send a POST request to a web service that needs dynamic multiple fields.

I mean, we need to send something like this as POST request:

question1='answer1'&question2='answer1'&question2='answer2'&question3='answer1'

where question1 and question2 are not set in compilation time. We know that we can use a @FieldMap for using dynamic fields, but we cannot send the same field more than one time.

This is our Retrofit code:

@FormUrlEncoded
@POST("/desafios/send/")
Observable<BaseServerMsgArray> postSubmitSurvey(@Field("customerId") Long customerId, @Field("upload_from_app") int uploadFromApp, @FieldMap HashMap<String, ArrayList<String>> hashFields);

Could anyone help us?

Thanks in advance,

1
  • you can send a json string as argument in instead of field map Commented Jun 29, 2016 at 8:54

2 Answers 2

8

Prepare HashMap like below, put only those questaion that user answered.

 HashMap<String, String> map = new HashMap<>();
 map.put("question1", answer1);
 map.put("question2", answer2);
 map.put("question3", answer3);

change your retrofit as below

@FormUrlEncoded
@POST("/desafios/send/")
Observable<BaseServerMsgArray> postSubmitSurvey(@Field("customerId") Long customerId, @Field("upload_from_app") int uploadFromApp, @FieldMap HashMap<String, String> hashFields);

and pass above HashMap as last argument

4
  • 1
    Thank you for your answer, but that's not my question. I need to send more than one "question2" value.
    – Juzmulti
    Commented Jun 29, 2016 at 9:00
  • If you need to send more than one question2 value..then it sounds like you need an array...something that would get converted into question2[]=$MY_FIRST_ARRAY_VAL&question2[]=$MY_SECOND_ARRAY_VAL.. have you tried passing a hashmap with String keys and a String[]..or a List?
    – snkashis
    Commented Jun 29, 2016 at 15:42
  • 1
    Yes, I tried, but the output format does not match, it has a list separated by commas. Thank you.
    – Juzmulti
    Commented Jul 4, 2016 at 12:44
  • @Juzmulti have you got solution for this? Commented Dec 9, 2017 at 4:44
1

What you need is a map that can contain duplicate keys. Unfortunately there is no such map in standard Android. You might use this onehttps://github.com/greenrobot/essentials/blob/master/java-essentials/src/main/java/org/greenrobot/essentials/collections/Multimap.java

If this won't work, try to manually convert your questions to String and send properly formatted String.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.