如何使用Retrofit发送数组/列表 [英] How to send Arrays / Lists with Retrofit
问题描述
我需要将一个列表/一组Integer值与Retrofit一起发送到服务器(通过POST)
我这样做:
@FormUrlEncoded
@POST(/ profile / searchProfile)
Call< ResponseBody> postSearchProfile(
@Field(age)列表<整数>年龄
};
并像这样发送:
ArrayList< Integer> ages = new ArrayList<>();
ages.add(20);
ages.add(30);
ISearchProfilePost iSearchProfile = gsonServerAPIRetrofit.create(ISearchProfilePost.class);
Call< ResponseBody> call = iSearchProfile .postSearchProfile(
年龄
);
问题是,价值达不到服务器不是以逗号分隔。所以这里的值就像年龄:2030 而不是年龄:20,30 。
<我正在阅读(例如 https://stackoverflow.com/a/37254442/1565635 )通过使用[]编写参数,就像一个数组,但只导致名为 age []的参数:2030 。
我也尝试过使用数组以及带字符串的列表。同样的问题。一切都直接在一个条目中。
那我该怎么办?
作为对象发送
这是你的ISearchProfilePost.class
@FormUrlEncoded
@POST(/ profile / searchProfile)
Call< ResponseBody> postSearchProfile(@Body ArrayListAge年龄);
在这里,您将在pojo类中输入发布数据
public class ArrayListAge {
@SerializedName(age)
@Expose
private ArrayList< String>年龄;
public ArrayListAge(ArrayList< String> ages){
this.ages = ages;
}
}
您的改装电话课程
ArrayList< Integer> ages = new ArrayList<>();
ages.add(20);
ages.add(30);
ArrayListAge arrayListAge = new ArrayListAge(ages);
ISearchProfilePost iSearchProfile = gsonServerAPIRetrofit.create(ISearchProfilePost.class);
电话< ResponseBody> call = iSearchProfile.postSearchProfile(arrayListAge);
要作为数组列表发送,请检查此链接 https://github.com/square/retrofit/issues/1064
你忘记添加年龄[]
@FormUrlEncoded
@ POST(/ profile / searchProfile)
Call< ResponseBody> postSearchProfile(
@Field(age [])List< Integer> age
};
I need to send a list / an array of Integer values with Retrofit to the server (via POST) I do it this way:
@FormUrlEncoded
@POST("/profile/searchProfile")
Call<ResponseBody> postSearchProfile(
@Field("age") List<Integer> age
};
and send it like this:
ArrayList<Integer> ages = new ArrayList<>();
ages.add(20);
ages.add(30);
ISearchProfilePost iSearchProfile = gsonServerAPIRetrofit.create(ISearchProfilePost.class);
Call<ResponseBody> call = iSearchProfile.postSearchProfile(
ages
);
The problem is, the values reach the server not comma separated. So the values there are like age: 2030 instead of age: 20, 30.
I was reading (e.g. here https://stackoverflow.com/a/37254442/1565635) that some had success by writing the parameter with [] like an array but that leads only to parameters called age[] : 2030. I also tried using Arrays as well as Lists with Strings. Same problem. Everything comes directly in one entry.
So what can I do?
To send as an Object
This is your ISearchProfilePost.class
@FormUrlEncoded
@POST("/profile/searchProfile")
Call<ResponseBody> postSearchProfile(@Body ArrayListAge ages);
Here you will enter the post data in pojo class
public class ArrayListAge{
@SerializedName("age")
@Expose
private ArrayList<String> ages;
public ArrayListAge(ArrayList<String> ages) {
this.ages=ages;
}
}
Your retrofit call class
ArrayList<Integer> ages = new ArrayList<>();
ages.add(20);
ages.add(30);
ArrayListAge arrayListAge = new ArrayListAge(ages);
ISearchProfilePost iSearchProfile = gsonServerAPIRetrofit.create(ISearchProfilePost.class);
Call<ResponseBody> call = iSearchProfile.postSearchProfile(arrayListAge);
To send as an Array List check this link https://github.com/square/retrofit/issues/1064
You forget to add age[]
@FormUrlEncoded
@POST("/profile/searchProfile")
Call<ResponseBody> postSearchProfile(
@Field("age[]") List<Integer> age
};
这篇关于如何使用Retrofit发送数组/列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!