gpt4 book ai didi

android - 在 multipart retrofit2 中上传图像数组

转载 作者:搜寻专家 更新时间:2023-11-01 08:26:44 25 4
gpt4 key购买 nike

我需要使用多部分在服务器上上传一组图像。我还想为该数组提供 key ,否则服务器无法识别该数组。我已经尝试了以下解决方案,但这些都不起作用:解决方案 1:

 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 传递带有键名的图像数组。

最佳答案

我的请求方式:

@Multipart
@POST("admin/assets/upload/")
Observable<UploadResponse> uploadFiles(@Part MultipartBody.Part... files);

从 uri 数组创建部件。所有部分都将具有 key “files[]”,这是我的后端识别的:

//UriHandle and OkioUtil are my own classes to handle both 'content' and 'file' uris
//files has type of Uri[]
final MultipartBody.Part[] parts = new MultipartBody.Part[files.length];
for (int i = 0; i < files.length; i++) {
final UriHandle handle = UriUtil.getUriHandle(context, files[i]);
final RequestBody body = OkioUtil.create(handle, mediaType);
parts[i] = MultipartBody.Part.createFormData("files[]", handle.name(), body);
}

发送请求:

api.uploadFiles(part)
.subscribeOn(...)
.observeOn(...)
.subscribe(result -> ...);

关于android - 在 multipart retrofit2 中上传图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44298417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com