Following is the multipart request that i am performing using retrofit2
.
What i want is to somehow get a callback of the upload percentage
. But i cannot a find a suitable method for doing this. I have saw few methods but they are only playing with single
file. What i want is to achieve is get a upload progress percentage of combine all files sent in the request.
@Multipart
@POST("post/vehicle/detail")
Call<SuccessModel> uploadVehicle(
@Part("user_id") int userId,
@Part("name") String vehicleName,
@Part("number") int stockNumber,
@Part MultipartBody.Part mainImage,
@Part MultipartBody.Part hoodsImage,
@Part MultipartBody.Part bumperImage,
@Part MultipartBody.Part interiorImage,
@Part MultipartBody.Part frontSeatImage,
@Part MultipartBody.Part backSeatImage,
@Part MultipartBody.Part dashboardImage,
@Part MultipartBody.Part headLampsImage,
@Part MultipartBody.Part tailLightsImage,
@Part MultipartBody.Part backEngineImage
);
Edit
Some more code about How i am creating Request.
File mainImage = new File(vehicleEntity.mainImage);
RequestBody requestMainFile = RequestBody.create(MediaType.parse("multipart/form-data"), mainImage);
MultipartBody.Part main = MultipartBody.Part.createFormData("main_image", mainImage.getName(), requestMainFile);
File hoodImage, bumperImage, interiorImage, frontSeatImage, backSeatImage, dashboardImage, headLampsImage, tailLightImage, backEngineImage;
MultipartBody.Part hood = null, bumper = null, interior = null, frontSeat = null, backSeat = null, dashboard = null, headLamps = null, tailLights = null, backEngine = null;
if (vehicleEntity.hoodsImage != null) {
hoodImage = new File(vehicleEntity.hoodsImage);
RequestBody requestHoodFile = RequestBody.create(MediaType.parse("multipart/form-data"), hoodImage);
hood = MultipartBody.Part.createFormData("hoods", hoodImage.getName(), requestHoodFile);
}
if (vehicleEntity.bumperImage != null) {
bumperImage = new File(vehicleEntity.bumperImage);
RequestBody requestBumperFile = RequestBody.create(MediaType.parse("multipart/form-data"), bumperImage);
bumper = MultipartBody.Part.createFormData("bumpers", mainImage.getName(), requestBumperFile);
}
if (vehicleEntity.interiorImage != null) {
interiorImage = new File(vehicleEntity.interiorImage);
RequestBody requestInteriorFile = RequestBody.create(MediaType.parse("multipart/form-data"), interiorImage);
interior = MultipartBody.Part.createFormData("interiors", mainImage.getName(), requestInteriorFile);
}
if (vehicleEntity.frontSeatImage != null) {
frontSeatImage = new File(vehicleEntity.frontSeatImage);
RequestBody requestFrontSeatFile = RequestBody.create(MediaType.parse("multipart/form-data"), frontSeatImage);
frontSeat = MultipartBody.Part.createFormData("front_seat", mainImage.getName(), requestFrontSeatFile);
}
if (vehicleEntity.backSeatImage != null) {
backSeatImage = new File(vehicleEntity.backSeatImage);
RequestBody requestBackSeatFile = RequestBody.create(MediaType.parse("multipart/form-data"), backSeatImage);
backSeat = MultipartBody.Part.createFormData("back_seat", mainImage.getName(), requestBackSeatFile);
}
if (vehicleEntity.dashboardImage != null) {
dashboardImage = new File(vehicleEntity.dashboardImage);
RequestBody requestDashboardFile = RequestBody.create(MediaType.parse("multipart/form-data"), dashboardImage);
dashboard = MultipartBody.Part.createFormData("dashboard", mainImage.getName(), requestDashboardFile);
}
if (vehicleEntity.headLampsImage != null) {
headLampsImage = new File(vehicleEntity.headLampsImage);
RequestBody requestHeadLampsFile = RequestBody.create(MediaType.parse("multipart/form-data"), headLampsImage);
headLamps = MultipartBody.Part.createFormData("headlamps", mainImage.getName(), requestHeadLampsFile);
}
if (vehicleEntity.tailLightsImage != null) {
tailLightImage = new File(vehicleEntity.tailLightsImage);
RequestBody requestTailsLightFile = RequestBody.create(MediaType.parse("multipart/form-data"), tailLightImage);
tailLights = MultipartBody.Part.createFormData("tail_light", mainImage.getName(), requestTailsLightFile);
}
if (vehicleEntity.backEngineImage != null) {
backEngineImage = new File(vehicleEntity.backEngineImage);
RequestBody requestBackEngineFile = RequestBody.create(MediaType.parse("multipart/form-data"), backEngineImage);
backEngine = MultipartBody.Part.createFormData("tail_light", mainImage.getName(), requestBackEngineFile);
}
int userId = Integer.parseInt(sharedPreferences.getString(USER_ID, null));
String vehicleName = vehicleEntity.vehicleName;
int stockNumber = Integer.parseInt(vehicleEntity.stockNumber);
Call<SuccessModel> call = apiInterface.uploadVehicle(
userId, vehicleName, stockNumber, main,
hood, bumper, interior, frontSeat, backSeat,
dashboard, headLamps, tailLights, backEngine
);