如果我有这个:
RequestBody formBody = new FormEncodingBuilder()
.add("email", "Jurassic@Park.com")
.add("tel", "90301171XX")
.build();
但是我没有单独添加键值对,而只是想添加一个具有可变大小的map类型的变量,我该如何添加呢?
答案 0 :(得分:2)
如何自己迭代地图并添加每个键/值?例如:
private FormEncodingBuilder makeBuilderFromMap(final Map<String, String> map) {
FormEncodingBuilder formBody = new FormEncodingBuilder();
for (final Map.Entry<String, String> entrySet : map.entrySet()) {
formBody.add(entrySet.getKey(), entrySet.getValue());
}
return formBody;
}
用法:
RequestBody body = makeBuilderFromMap(map)
.otherBuilderStuff()
.otherBuilderStuff()
.otherBuilderStuff()
.build();
答案 1 :(得分:1)
如果您正确输入,nbokmans提供的代码效果很好。这里是更正后的版本:
private RequestBody makeFormBody(final Map<String, String> map) {
FormEncodingBuilder formBody = new FormEncodingBuilder();
for (final Map.Entry<String, String> entrySet : map.entrySet()) {
formBody.add(entrySet.getKey(), entrySet.getValue());
}
return formBody.build();
}