I must send multipart request in which one part is array of images.
For example:
{
"name": "Frying Pan Tower",
"lat": 33.747358867853706,
"lng": -77.92252913117409,
"depth": 15.24,
"images[]" - to this field i need paste a array of images
}
I'm using Retrofit 2.0.1
For one image i create something like this, and it's work fine for me
@Multipart
Call<ResponseBody> add(
@Part("name") RequestBody name,
@Part("distribution") RequestBody distribution,
@Part("habitat") RequestBody habitat,
@Part MultipartBody.Part image);
You can use @Part List<MultipartBody.Part> images for a runtime-defined number of parts.
@lashket hi! May you say, how you solved your problem? I cant send list of images :(
hi @JakeWharton, I also tried this approach
@Multipart
@POST(Endpoints.BASE_URL + Endpoints.EVENTS)
Call<ResponseBody> createTrip(@Header("Authorization") String header,
@Part("access_token") String access,
@Part List<MultipartBody.Part> photos,
@Part("name") RequestBody trip_name,
@Part("start_at") RequestBody start_at,
@Part("end_at") RequestBody end_at,
@Part("downpayment_rate") RequestBody downpayment,
@Part("booking_fee") RequestBody booking_fee,
@Part("meeting_place") RequestBody meeting_place,
@Part List<MultipartBody.Part> itinerary,
@Part("min_pax") RequestBody min_pax,
@Part("max_pax") RequestBody max_pax,
@Part("lat") RequestBody lat,
@Part("lng") RequestBody lng);
@Part List<MultipartBody.Part> itinerary is actually working but @Part List<MultipartBody.Part> photos is not.
This is how I created them
for (int i = 0; i < coverImages.size(); i++) {
File file = new File(coverImages.get(i).path);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("photos[]", file.getName(), requestBody);
photos.add(filePart);
}
`for (int i = 0; i < etListItinerary.size(); i++) {
MultipartBody.Part part = MultipartBody.Part.createFormData("itineraries_description[]", etListItinerary.get(i).getText().toString());
itineraries_description.add(part);
}`
@mengmengmengmeng did you manage to resolve this issue? i'm having troubles to send a header, a json object and an array of images to my API. My code looks very similar to yours, did it worked?
hi there , if any one still couldn't solve this issue ,,, try this
@Multipart
Call
@Part("name") RequestBody name,
@Part("distribution") RequestBody distribution,
@Part("habitat") RequestBody habitat,
@Part MultipartBody.Part image,
@Part List
);
You can use
@Part List<MultipartBody.Part> imagesfor a runtime-defined number of parts.
@JakeWharton @alex8530 it seems there is a constraint up to 20 images in order to upload. I've tried to send a HTTP request with more than 20 attached images as multipart-data through com.squareup.retrofit2:retrofit:2.2.0 and com.squareup.okhttp3:logging-interceptor:3.4.1
There is no constraint in either Retrofit or OkHttp so anything imposed will be done by the target server.
Most helpful comment
You can use
@Part List<MultipartBody.Part> imagesfor a runtime-defined number of parts.