[英]Retrofit string array sending object representation instead of value in request.
I am using Retrofit 2 with Jackson converter in my Android project with the following request: 我在Android项目中使用带有Jackson转换器的Retrofit 2和以下请求:
@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings") List<String> array);
Here are the values that I pass to the interface method: 这是我传递给接口方法的值:
// the array of strings @Field param
ArrayList<String> arrayOfStrings = new ArrayList<>();
arrayOfStrings.add("1.2");
// the id param
int id = 2;
I run the app, have the API call perform the request, then retrieve the request via Charles . 我运行应用程序,让API调用执行请求,然后通过Charles检索请求。
Here is the issue. 这是问题。 The request param "array_of_strings" is being sent using the object reference, not the actual value of the array objects:
请求参数“ array_of_strings”是使用对象引用而不是数组对象的实际值发送的:
// charles request shows:
array_of_strings [I@41fea508
// instead of:
array_of_stings ["1.2"]
How do I get Retrofit/Jackson to use the value of the array objects instead of the object reference value? 如何让Retrofit / Jackson使用数组对象的值而不是对象引用值?
I guess you should just add []
brackets to array_of_strings
field name, so Retrofit will know that you're going to send an array and will properly transform it to array field: 我想您应该在
array_of_strings
字段名称中添加[]
括号,这样Retrofit就会知道您将要发送一个数组并将其正确转换为数组字段:
@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings[]") List<String> array);
Via rom4ek's comment in this answer, using ArrayList<String>
as the parameter type was the solution. 通过使用rom4ek在此答案中的注释,可以使用
ArrayList<String>
作为参数类型。 Jackson must serialize ArrayLists differently than arrays or Lists. Jackson必须对ArrayLists进行序列化的方式与对数组或List进行序列化的方式不同。
@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings") ArrayList<String> array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.