So its very simple way to achieve your task. You need to follow below step :-
1. First step
public interface APIService {
@Multipart
@POST("upload")
Call<ResponseBody> upload(
@Part("item") RequestBody description,
@Part("imageNumber") RequestBody description,
@Part MultipartBody.Part imageFile
);
}
You need to make the entire call as @Multipart request
. item
and image number
is just string body which is wrapped in RequestBody
. We use the MultipartBody.Part class
that allows us to send the actual file name besides the binary file data with the request
2. Second step
File file = (File) params[0];
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part body =MultipartBody.Part.createFormData("Image", file.getName(), requestBody);
RequestBody ItemId = RequestBody.create(okhttp3.MultipartBody.FORM, "22");
RequestBody ImageNumber = RequestBody.create(okhttp3.MultipartBody.FORM,"1");
final Call<UploadImageResponse> request = apiService.uploadItemImage(body, ItemId,ImageNumber);
Now you have image path
and you need to convert into file
.Now convert file
into RequestBody
using method RequestBody.create(MediaType.parse("multipart/form-data"), file)
. Now you need to convert your RequestBody requestFile
into MultipartBody.Part
using method MultipartBody.Part.createFormData("Image", file.getName(), requestBody);
.
ImageNumber
and ItemId
is my another data which I need to send to server so I am also make both thing into RequestBody
.