[英]Tricky @Field description for RetroFit FormUrlEncoded parameter using a List of Objects
I am performing a @POST operation using @FormUrlEncoded, because the API i'm hitting requires HTTP params as input. 我正在使用@FormUrlEncoded执行@POST操作,因为我要访问的API需要HTTP参数作为输入。
The params the target API are expecting to see should look like (simplified): 目标API期望看到的参数应类似于(简化):
alist[0][object1]=1&alist[0][object2]=astring&alist[1][object1]=2&alist[2][object2]=bstring alist [0] [object1] = 1&alist [0] [object2] = astring&alist [1] [object1] = 2&alist [2] [object2] = bstring
JsonIzed (for clarity) looks like: JsonIzed(为清楚起见)如下所示:
{
"alist": [
{
"object1" : "1",
"object2" : "astring"
},
{
"object1" : "2",
"object2" : "bstring"
}
]
}
So essentially, alist[0][object1] is 1, alist[0][object2] is "astring". 因此,本质上,alist [0] [object1]为1,alist [0] [object2]为“字符串”。
The closest I've gotten is using @FieldMap: 我最近得到的是使用@FieldMap:
...
@FieldMap Map<String,String> myFieldMap
...
I have something like this: 我有这样的事情:
@FormUrlEncoded
@Post("/somewhere")
SearchHandle search(@FieldMap Map<String, String> myMap);
Which gets me: 这让我:
{
"alist": {
"0" : {
"object1" : "1",
"object2" : "astring"
},
"1" : {
"object1" : "2",
"object2" : "bstring"
}
}
}
Which is close - but the API is expecting an Array. 哪一个很接近-但API需要一个数组。 So I need an Array of ... what exactly? 所以我需要一个...的数组到底是什么? Any ideas? 有任何想法吗?
create a class for modeling your data : 创建一个用于对数据建模的类:
class model {
String object1;
int object2;
}
now your alist should be an array of model objects and you can create json object from your array using Gson or jackson or etc. 现在您的清单应该是一个模型对象数组,您可以使用Gson或jackson等从数组中创建json对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.