Search in sources :

Example 1 with MultipartFileBody

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();
}
Also used : MultipartStringBody(com.androidnetworking.model.MultipartStringBody) HashMap(java.util.HashMap) MultipartFileBody(com.androidnetworking.model.MultipartFileBody) MultipartBody(okhttp3.MultipartBody) MediaType(okhttp3.MediaType) List(java.util.List) ArrayList(java.util.ArrayList) RequestBody(okhttp3.RequestBody)

Aggregations

MultipartFileBody (com.androidnetworking.model.MultipartFileBody)1 MultipartStringBody (com.androidnetworking.model.MultipartStringBody)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 MediaType (okhttp3.MediaType)1 MultipartBody (okhttp3.MultipartBody)1 RequestBody (okhttp3.RequestBody)1