我可以发送一个包含3个图像和一些属性的MultipartBody,如下所示
接口:
@POST("test_request")
Call<ResponseBody> test(@Body RequestBody file);
代码:
private MultipartBody getMultiFile(CoursesAdded coursesAdded) {
ArrayList<String> filePaths = new ArrayList<>();
filePaths.add(coursesAdded.getImag1());
filePaths.add(coursesAdded.getImag2());
filePaths.add(coursesAdded.getImag3());
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("skill", coursesAdded.getCourse_name());
builder.addFormDataPart("certificate_from", coursesAdded.getCourse_location());
builder.addFormDataPart("certificate_date", "null");
for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
builder.addFormDataPart("file[]", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));
}
MultipartBody requestBody = builder.build();
return requestBody;
}
但现在我需要发送MultipartBody数组。
获取数组函数:
private MultipartBody[] getArray() {
MultipartBody[] multipartBodies = new MultipartBody[coursesAddedArrayList.size()];
for (int i = 0; i < coursesAddedArrayList.size(); i++) {
CoursesAdded coursesAdded = new CoursesAdded(
coursesAddedArrayList.get(i).getCourse_name(),
coursesAddedArrayList.get(i).getCourse_location(),
coursesAddedArrayList.get(i).getImag1(),
coursesAddedArrayList.get(i).getImag2(),
coursesAddedArrayList.get(i).getImag3()
);
multipartBodies[i] = getMultiFile(coursesAdded);
}
return multipartBodies;
}
接口:
@POST("test_request")
Call<ResponseBody> addCourses(
@Body MultipartBody[] requestsBody
);
当显示请求以响应查看发送的内容时,我得到以下内容
[size = 1838 text = {&#34; test&#34;:[{&#34; boundary&#34;:{&#34; data&#34;:[55,48,99,55,97] ,56,52,53,45,101,97,...]