我们需要向需要动态多个字段的网络服务发送 POST 请求。
我的意思是,我们需要发送这样的东西作为 POST 请求:
question1='answer1'&question2='answer1'&question2='answer2'&question3='answer1'
其中question1和question2在编译时没有设置。我们知道我们可以使用 @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);
谁能帮帮我们?
提前致谢
最佳答案
像下面这样准备HashMap,只放那些用户回答过的问题。
HashMap<String, String> map = new HashMap<>();
map.put("question1", answer1);
map.put("question2", answer2);
map.put("question3", answer3);
如下更改改造
@FormUrlEncoded
@POST("/desafios/send/")
Observable<BaseServerMsgArray> postSubmitSurvey(@Field("customerId") Long customerId, @Field("upload_from_app") int uploadFromApp, @FieldMap HashMap<String, String> hashFields);
并在 HashMap 之上作为最后一个参数传递
关于android - 使用 Retrofit 发送多个动态字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094579/