[英]Sending array list in a POST request using Retrofit sends the object with the memory address instead of sending the values
[英]Retrofit string array sending object representation instead of value in request.
我在Android项目中使用带有Jackson转换器的Retrofit 2和以下请求:
@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings") List<String> array);
这是我传递给接口方法的值:
// the array of strings @Field param
ArrayList<String> arrayOfStrings = new ArrayList<>();
arrayOfStrings.add("1.2");
// the id param
int id = 2;
我运行应用程序,让API调用执行请求,然后通过Charles检索请求。
这是问题。 请求参数“ array_of_strings”是使用对象引用而不是数组对象的实际值发送的:
// charles request shows:
array_of_strings [I@41fea508
// instead of:
array_of_stings ["1.2"]
如何让Retrofit / Jackson使用数组对象的值而不是对象引用值?
我想您应该在array_of_strings
字段名称中添加[]
括号,这样Retrofit就会知道您将要发送一个数组并将其正确转换为数组字段:
@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings[]") List<String> array);
通过使用rom4ek在此答案中的注释,可以使用ArrayList<String>
作为参数类型。 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.