use of org.xutils.http.body.FileBody in project xUtils3 by wyouflf.
the class BaseParams method getRequestBody.
public RequestBody getRequestBody() throws IOException {
checkBodyParams();
if (this.requestBody != null) {
return this.requestBody;
}
RequestBody result = null;
if (!TextUtils.isEmpty(bodyContent)) {
result = new StringBody(bodyContent, charset);
} else if (multipart || fileParams.size() > 0) {
if (!multipart && fileParams.size() == 1) {
for (KeyValue kv : fileParams) {
String contentType = null;
Object value = kv.value;
if (value instanceof BodyItemWrapper) {
BodyItemWrapper wrapper = (BodyItemWrapper) value;
value = wrapper.getValue();
contentType = wrapper.getContentType();
}
if (value instanceof File) {
result = new FileBody((File) value, contentType);
} else if (value instanceof InputStream) {
result = new InputStreamBody((InputStream) value, contentType);
} else if (value instanceof byte[]) {
result = new InputStreamBody(new ByteArrayInputStream((byte[]) value), contentType);
} else if (value instanceof String) {
// invoke addBodyParameter(key, stringValue, contentType)
result = new StringBody((String) value, charset);
result.setContentType(contentType);
} else {
LogUtil.w("Some params will be ignored for: " + this.toString());
}
break;
}
} else {
multipart = true;
result = new MultipartBody(fileParams, charset);
}
} else if (bodyParams.size() > 0) {
result = new UrlEncodedParamsBody(bodyParams, charset);
}
return result;
}
Aggregations