简体   繁体   中英

Retrofit 2 Upload Image Multiple

        "gallery": [
            {
                "galleryId": 113,
                "type": "image",
                "filename": "image_599bbbb569b78.png"
            },
            {
                "galleryId": 114,
                "type": "image",
                "filename": "image_599bbdd023a31.png"
            }
        ],
        "likeCount": 2,

I don't know how to use retrofit 2 to upload image like this. can someone please share example. i can use retrofit2 upload in one-to-one field but one-to-many i don't know how to use.

Call<AnnouncementHeaderModel> announcement_add(@Part MultipartBody.Part[] filename
        ,@Part MultipartBody.Part[] type
        ,@PartMap() Map<String, RequestBody> partMap);

I solved my problem like this.

Edit: To upload data in below json format:

    {
  "gallery": [
            {
                "galleryId": 113,
                "type": "image",
                "filename": "image_599bbbb569b78.png"
            },
            {
                "galleryId": 114,
                "type": "image",
                "filename": "image_599bbdd023a31.png"
            }
        ],
   "likeCount": 2
}

you have to fill data in below POJO and send to retrofit body

   import java.util.List;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;

    public class UploadGallery {

    @SerializedName("gallery")
    @Expose
    private List<Gallery> gallery = null;
    @SerializedName("likeCount")
    @Expose
    private Integer likeCount;

    public List<Gallery> getGallery() {
    return gallery;
    }

    public void setGallery(List<Gallery> gallery) {
    this.gallery = gallery;
    }

    public Integer getLikeCount() {
    return likeCount;
    }

    public void setLikeCount(Integer likeCount) {
    this.likeCount = likeCount;
    }

    public class Gallery {

    @SerializedName("galleryId")
    @Expose
    private Integer galleryId;
    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("filename")
    @Expose
    private String filename;

    public Integer getGalleryId() {
    return galleryId;
    }

    public void setGalleryId(Integer galleryId) {
    this.galleryId = galleryId;
    }

    public String getType() {
    return type;
    }

    public void setType(String type) {
    this.type = type;
    }

    public String getFilename() {
    return filename;
    }

    public void setFilename(String filename) {
    this.filename = filename;
    }

    }
}

This will create a list of objects of gallery just like you want to send. I hope this solves your problem.

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