使用Retrofit发送多个动态字段

Juz*_*lti 8 android retrofit retrofit2

我们需要向需要动态多个字段的Web服务发送POST请求.

我的意思是,我们需要发送类似POST的请求:

question1='answer1'&question2='answer1'&question2='answer2'&question3='answer1'
Run Code Online (Sandbox Code Playgroud)

其中question1和question2未在编译时设置.我们知道我们可以使用@FieldMap来使用动态字段,但是我们不能多次发送同一个字段.

这是我们的改造代码:

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

有人可以帮助我们吗?

提前致谢,

San*_*iya 7

像下面那样准备HashMap,只放入用户回答的那些问题。

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

如下更改您的改造

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

并传递到HashMap上方作为最后一个参数