Android Retrofit POST ArrayList

删除回忆录丶 提交于 2019-12-02 04:57:55

问题


Trying to send List<String> to server and have Bad Request Error.

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) {
                }
            });
        }

Make @Field("review[tag_list[]]") doesn't help either


回答1:


Try using @Body annotation instead of @Field and passing a single ReviewBody object.

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);

(without @FormUrlEncoded)




回答2:


@Field annotations is used for form-encoded request. Egs,

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

However you wont be able to send ArrayList tagList using @Field annotation.

Instead create a Config class and annotate the class variables as @SerializedName ("XYZ") followed by variable name to enable serialization of Array List in Retrofit. Please find the below code for reference,

  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;
  }

Further you can define your interface as follows,

@POST("/") Call<ReviewEntity> addReview(@Body Hotels hotels);

Hope this might help in future prospects



来源:https://stackoverflow.com/questions/32224092/android-retrofit-post-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!