Android 改造 POST ArrayList
Android Retrofit POST ArrayList
正在尝试发送 List<String>
到服务器并出现错误请求错误。
public interface PostReviewApi {
@FormUrlEncoded
@POST("/")
void addReview(@Field("review[place_id]") int placeId, @Field("review[content]") String review,
@Field("review[rating]") float rating, @Field("review[tag_list]") List<String> tagsList, Callback<ReviewEntity> callback);
}
...
postApi.addReview(mRestaurantId, reviewText, (float) mRating, tagsList, new Callback<ReviewEntity>() {
@Override
public void success(ReviewEntity reviewEntity, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
});
}
使@Field("review[tag_list[]]")
也无济于事
尝试使用 @Body
注释而不是 @Field
并传递单个 ReviewBody
对象。
class ReviewBody {
public Review review;
public ReviewBody(int placeId, float rating, String content, List<String> tagList) {
review = new Review(placeId, rating, content, tagList);
}
public class Review {
@SerializedName("place_id")
public int placeId;
public float rating;
public String content;
@SerializedName("tag_list")
public List<String> tagList;
public Review(int placeId, float rating, String content, List<String> tagList) {
this.placeId = placeId;
this.rating = rating;
this.content = content;
this.tagList = tagList;
}
}
}
@POST("/")
void addReview(@Body ReviewBody body, Callback<ReviewEntity> callback);
(没有@FormUrlEncoded
)
@字段注解用于表单编码请求。例如,
@FormUrlEncoded
@POST("/")
Call<ResponseBody> example(
@Field("name") String name,
@Field("occupation") String occupation);
Calling with foo.example("Bob Smith", "President") yields a request body of name=Bob+Smith&occupation=President.
但是您将无法使用@Field 注释发送ArrayList tagList。
而是创建一个配置 class 并将 class 变量注释为 @SerializedName ("XYZ") 后跟变量名称以启用 Retrofit 中数组列表的序列化。请找到以下代码以供参考,
public class Hotels {
private @SerializedName("place_id") int mRestaurantId;
private @SerializedName("content") String review;
private @SerializedName("rating") float rating;
private @SerializedName("tag_list") List<String> tagsList;
public int getmRestaurantId() {
return mRestaurantId;
}
public void setmRestaurantId(int mRestaurantId) {
this.mRestaurantId = mRestaurantId;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
public List<String> getTagsList() {
return tagsList;
}
public void setTagsList(List<String> tagsList) {
this.tagsList = tagsList;
}
您还可以定义您的界面,如下所示,
@POST("/") Call<ReviewEntity> addReview(@Body Hotels hotels);
希望这对未来的前景有所帮助
在Multipart
的情况下:
@Part("item_ids[]") items : List<Int>
完整示例:
假设您要注册(发送用户数据 - 上传任何图片和列表)
@Multipart
@POST(REGISTER_LINK)
suspend fun register(
@PartMap partMap: MutableMap<String, RequestBody>, // (name, email..etc)
@Part file: MultipartBody.Part?, // image file
@Part("item_ids[]") listOf: List<Int> // list of
): NetworkResponse<>
正在尝试发送 List<String>
到服务器并出现错误请求错误。
public interface PostReviewApi {
@FormUrlEncoded
@POST("/")
void addReview(@Field("review[place_id]") int placeId, @Field("review[content]") String review,
@Field("review[rating]") float rating, @Field("review[tag_list]") List<String> tagsList, Callback<ReviewEntity> callback);
}
...
postApi.addReview(mRestaurantId, reviewText, (float) mRating, tagsList, new Callback<ReviewEntity>() {
@Override
public void success(ReviewEntity reviewEntity, Response response) {
}
@Override
public void failure(RetrofitError error) {
}
});
}
使@Field("review[tag_list[]]")
也无济于事
尝试使用 @Body
注释而不是 @Field
并传递单个 ReviewBody
对象。
class ReviewBody {
public Review review;
public ReviewBody(int placeId, float rating, String content, List<String> tagList) {
review = new Review(placeId, rating, content, tagList);
}
public class Review {
@SerializedName("place_id")
public int placeId;
public float rating;
public String content;
@SerializedName("tag_list")
public List<String> tagList;
public Review(int placeId, float rating, String content, List<String> tagList) {
this.placeId = placeId;
this.rating = rating;
this.content = content;
this.tagList = tagList;
}
}
}
@POST("/")
void addReview(@Body ReviewBody body, Callback<ReviewEntity> callback);
(没有@FormUrlEncoded
)
@字段注解用于表单编码请求。例如,
@FormUrlEncoded
@POST("/")
Call<ResponseBody> example(
@Field("name") String name,
@Field("occupation") String occupation);
Calling with foo.example("Bob Smith", "President") yields a request body of name=Bob+Smith&occupation=President.
但是您将无法使用@Field 注释发送ArrayList tagList。
而是创建一个配置 class 并将 class 变量注释为 @SerializedName ("XYZ") 后跟变量名称以启用 Retrofit 中数组列表的序列化。请找到以下代码以供参考,
public class Hotels {
private @SerializedName("place_id") int mRestaurantId;
private @SerializedName("content") String review;
private @SerializedName("rating") float rating;
private @SerializedName("tag_list") List<String> tagsList;
public int getmRestaurantId() {
return mRestaurantId;
}
public void setmRestaurantId(int mRestaurantId) {
this.mRestaurantId = mRestaurantId;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
public float getRating() {
return rating;
}
public void setRating(float rating) {
this.rating = rating;
}
public List<String> getTagsList() {
return tagsList;
}
public void setTagsList(List<String> tagsList) {
this.tagsList = tagsList;
}
您还可以定义您的界面,如下所示,
@POST("/") Call<ReviewEntity> addReview(@Body Hotels hotels);
希望这对未来的前景有所帮助
在Multipart
的情况下:
@Part("item_ids[]") items : List<Int>
完整示例:
假设您要注册(发送用户数据 - 上传任何图片和列表)
@Multipart
@POST(REGISTER_LINK)
suspend fun register(
@PartMap partMap: MutableMap<String, RequestBody>, // (name, email..etc)
@Part file: MultipartBody.Part?, // image file
@Part("item_ids[]") listOf: List<Int> // list of
): NetworkResponse<>