1
我需要上傳服務器上的圖像數組使用多部分。我也想給該數組的密鑰,否則服務器無法識別數組。我曾嘗試解決以下,但這些不工作: 解決方法1:上傳圖像的多部分改造2
MultipartBody.Part[] array = new MultipartBody.Part[items.size()];
for(int i=0;i<items.size();i++){
File file = new File(items.get(i));
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("value_"
+ String.valueOf(uploadMissionRequestModel.getStepId()), file.getName(),
requestBody);
array[i]=filePart;
}
RequestBody apiKey = RequestBody.create(MediaType.parse("text/plain"),
uploadMissionRequestModel.getApiKey());
RequestBody stepId = RequestBody.create(MediaType.parse("text/plain"),
String.valueOf(uploadMissionRequestModel.getStepId()));
RequestBody missionId = RequestBody.create(MediaType.parse("text/plain"),
String.valueOf(uploadMissionRequestModel.getMissionId()));
RequestBody overWrite = RequestBody.create(MediaType.parse("text/plain"),
"yes");
Map<String, RequestBody> partMap = new HashMap<>();
partMap.put("api_token", apiKey);
partMap.put("MissionID", missionId);
partMap.put("overwrite", overWrite);
partMap.put("StepID", stepId);
ApiServices service = RestClient.getClient();
final Call<UploadMissionResponse> call = service.uploadFiles(partMap, array);
溶液2
RequestBody[] image_id = new RequestBody[items.size()];
for(int i=0;i<items.size();i++){
File file = new File(items.get(i));
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
image_id[i]=requestBody;
}
RequestBody apiKey = RequestBody.create(MediaType.parse("text/plain"),
uploadMissionRequestModel.getApiKey());
RequestBody stepId = RequestBody.create(MediaType.parse("text/plain"),
String.valueOf(uploadMissionRequestModel.getStepId()));
RequestBody missionId = RequestBody.create(MediaType.parse("text/plain"),
String.valueOf(uploadMissionRequestModel.getMissionId()));
RequestBody overWrite = RequestBody.create(MediaType.parse("text/plain"),
"yes");
Map<String, RequestBody> partMap = new HashMap<>();
partMap.put("api_token", apiKey);
partMap.put("MissionID", missionId);
partMap.put("overwrite", overWrite);
partMap.put("StepID", stepId);
Map<String, RequestBody[]> imageMap = new HashMap<>();
imageMap.put("value_8",image_id);
ApiServices service = RestClient.getClient();
final Call<UploadMissionResponse> call = service.uploadFiles(partMap, imageMap);
請建議,如何傳遞圖像陣列使用retrofit2鍵名。
非常感謝你。你拯救了我的一天。 – oalpayli