使用Retrofit发送多个动态字段

8
我们需要向一个需要动态多个字段的web服务发送POST请求。我的意思是,我们需要像这样发送POST请求:
question1='answer1'&question2='answer1'&question2='answer2'&question3='answer1'

当问题1和问题2在编译时未设置时,我们可以使用@FieldMap来使用动态字段,但是我们不能多次发送相同的字段。

这是我们的Retrofit代码:

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

有人可以帮助我们吗?

提前致谢,


你可以发送一个JSON字符串作为参数,而不是字段映射。 - SaravInfern
2个回答

8
准备一个类似下面的 HashMap,只放用户回答过的问题。
 HashMap<String, String> map = new HashMap<>();
 map.put("question1", answer1);
 map.put("question2", answer2);
 map.put("question3", answer3);

请将您的Retrofit更改如下:

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

并将上述 HashMap 作为最后一个参数传递。


1
谢谢您的回答,但那不是我的问题。我需要发送多个“question2”值。 - Juzmulti
如果您需要发送多个question2值...那么似乎您需要一个数组...这将被转换为question2[]=$MY_FIRST_ARRAY_VAL&question2[]=$MY_SECOND_ARRAY_VAL..您尝试过传递具有字符串键和字符串[]或列表的哈希映射吗? - snkashis
1
是的,我尝试过了,但输出格式不匹配,它有一个用逗号分隔的列表。谢谢。 - Juzmulti
@Juzmulti,你有解决这个问题的办法吗? - tranceholic

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接