use of com.androidnetworking.model.MultipartFileBody in project Fast-Android-Networking by amitshekhariitbhu.
the class ANRequest method getMultiPartRequestBody.
public RequestBody getMultiPartRequestBody() {
MultipartBody.Builder builder = new MultipartBody.Builder().setType((customMediaType == null) ? MultipartBody.FORM : customMediaType);
try {
for (HashMap.Entry<String, MultipartStringBody> entry : mMultiPartParameterMap.entrySet()) {
MultipartStringBody stringBody = entry.getValue();
MediaType mediaType = null;
if (stringBody.contentType != null) {
mediaType = MediaType.parse(stringBody.contentType);
}
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\""), RequestBody.create(mediaType, stringBody.value));
}
for (HashMap.Entry<String, List<MultipartFileBody>> entry : mMultiPartFileMap.entrySet()) {
List<MultipartFileBody> fileBodies = entry.getValue();
for (MultipartFileBody fileBody : fileBodies) {
String fileName = fileBody.file.getName();
MediaType mediaType;
if (fileBody.contentType != null) {
mediaType = MediaType.parse(fileBody.contentType);
} else {
mediaType = MediaType.parse(Utils.getMimeType(fileName));
}
RequestBody requestBody = RequestBody.create(mediaType, fileBody.file);
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + entry.getKey() + "\"; filename=\"" + fileName + "\""), requestBody);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.build();
}
Aggregations