Trying to send List
to server and have Bad Request Error.
public interface PostReviewApi {
@FormUrlEncoded
@POST(\
@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
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
)