Search in sources :

Example 1 with BodyItemWrapper

use of org.xutils.http.body.BodyItemWrapper 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;
}
Also used : KeyValue(org.xutils.common.util.KeyValue) FileBody(org.xutils.http.body.FileBody) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BodyItemWrapper(org.xutils.http.body.BodyItemWrapper) UrlEncodedParamsBody(org.xutils.http.body.UrlEncodedParamsBody) StringBody(org.xutils.http.body.StringBody) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.xutils.http.body.MultipartBody) InputStreamBody(org.xutils.http.body.InputStreamBody) JSONObject(org.json.JSONObject) File(java.io.File) RequestBody(org.xutils.http.body.RequestBody)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 JSONObject (org.json.JSONObject)1 KeyValue (org.xutils.common.util.KeyValue)1 BodyItemWrapper (org.xutils.http.body.BodyItemWrapper)1 FileBody (org.xutils.http.body.FileBody)1 InputStreamBody (org.xutils.http.body.InputStreamBody)1 MultipartBody (org.xutils.http.body.MultipartBody)1 RequestBody (org.xutils.http.body.RequestBody)1 StringBody (org.xutils.http.body.StringBody)1 UrlEncodedParamsBody (org.xutils.http.body.UrlEncodedParamsBody)1