简体   繁体   中英

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:

@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 .

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:

// 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?

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:

@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. Jackson must serialize ArrayLists differently than arrays or Lists.

@FormUrlEncoded
@PATCH("foo/{id}")
Call<FooModel> apiCallWithArrayOfStrings(@Path("id") int id, @Field("array_of_strings") ArrayList<String> array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM