本文介绍了如何在OkHttpClient中将对象(即UserData)类型的列表添加到MultipartBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象的ArrayList,我需要将其放入MultipartBody.Builder.我尝试使用.addFormDataPart的不同方法,但无法正常工作.这就是我将其放入请求中的方式.任何帮助,将不胜感激.谢谢
I have an ArrayList of Object that I need to put into my MultipartBody.Builder.I have tried using different methods of .addFormDataPart but not working. This is how I'm putting it into the request. Any help would be appreciated. Thanks
我在服务器上的JSON格式如下
My JSON on the server is formatted like this
{
"formData": [
{
"name": "string",
"value": {
any-object(string,file,long....etc)
}
}
],
"tripId": "string"
}
这就是我的做法
private RequestBody getMultipartData(Object postData) {
MultipartBody.Builder formBody = new MultipartBody.Builder();
formBody.setType(MultipartBody.FORM);
ArrayList<UserData> arrayList = null;
if (postData instanceof OnboardPresenterImpl.TripRequest) {
OnboardPresenterImpl.TripRequest tripRequest = ((OnboardPresenterImpl.TripRequest) postData);
formBody.addFormDataPart("tripId", tripRequest.getTripId());
arrayList = tripRequest.getFormData();
}
if (arrayList != null && arrayList.size() > 0) {
for (int i = 0; i < arrayList.size(); i++) {
UserData userData = arrayList.get(i);
String value = userData.getValue().toLowerCase();
if (value.contains("/") && (value.contains(".png") ||
value.contains(".jpeg") || value.contains(".jpg"))) {
MediaType type = Util.getMediaType(value);
File sourceFile = new File(value);
if (sourceFile.exists()) {
formBody.addFormDataPart(userData.getName(),
sourceFile.getName(),RequestBody.create(type, sourceFile));
}
} else {
formBody.addFormDataPart(userData.getName(), value);
}
}
}
return formBody.build();
}
推荐答案
几天后,我找出了解决方案
After a few days, I figure out the solution
String uploadMultipleFiles(Api api, HashMap<String, ArrayList<File>> fileListMap)
throws IOException {
String responseS;
OkHttpClient timeOut = getOkHttpClient(api.getTimeOut());
MultipartBody.Builder multipartBuilder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
int j = 0;
ArrayList<File> fileList;
for (Map.Entry<String, ArrayList<File>> entry : fileListMap.entrySet()) {
String key = entry.getKey();
fileList = entry.getValue();
multipartBuilder.addFormDataPart("data[" + j + "].name", key);
if (fileList != null) {
for (int i = 0; i < fileList.size(); i++) {
File fileListRequest = fileList.get(i);
if (fileListRequest.exists()) {
String ext = CommonUtils.getExtension(fileListRequest.getName());
/* Changing Media Type whether JPEG or PNG **/
final MediaType MEDIA_TYPE = ext.endsWith("png") ? MEDIA_TYPE_PNG :
ext.endsWith("jpg") ? MEDIA_TYPE_JPG : MEDIA_TYPE_JPEG;
multipartBuilder.addFormDataPart("data[" + j + "].files[" + i + "]",
fileListRequest.getName(),
RequestBody.create(MEDIA_TYPE, fileListRequest));
}
}
}
j++;
}
RequestBody requestBody = multipartBuilder.build();
Request.Builder requestBuilder = new Request.Builder()
.url(Objects.requireNonNull(api.getUrl()))
.post(requestBody);
buildHeaders(requestBuilder);
Response response = timeOut.newCall(requestBuilder.build()).execute();
responseS = response.body().string();
Log.i(api.getName().name(), responseS);
return responseS;
}
这篇关于如何在OkHttpClient中将对象(即UserData)类型的列表添加到MultipartBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!