Example usage for com.squareup.okhttp MultipartBuilder addPart

List of usage examples for com.squareup.okhttp MultipartBuilder addPart

Introduction

In this page you can find the example usage for com.squareup.okhttp MultipartBuilder addPart.

Prototype

public MultipartBuilder addPart(Headers headers, RequestBody body) 

Source Link

Document

Add a part to the body.

Usage

From source file:twitter4j.AlternativeHttpClientImpl.java

License:Apache License

private RequestBody getRequestBody(HttpRequest req) throws UnsupportedEncodingException {
    if (HttpParameter.containsFile(req.getParameters())) {
        final String boundary = "----Twitter4J-upload" + System.currentTimeMillis();
        MultipartBuilder multipartBuilder = new MultipartBuilder(boundary).type(MultipartBuilder.FORM);
        for (HttpParameter parameter : req.getParameters()) {
            if (parameter.isFile()) {
                multipartBuilder.addPart(
                        Headers.of("Content-Disposition",
                                "form-data; name=\"" + parameter.getName() + "\"; filename=\""
                                        + parameter.getFile().getName() + "\""),
                        RequestBody.create(MediaType.parse(parameter.getContentType()), parameter.getFile()));
            } else {
                multipartBuilder.addPart(
                        Headers.of("Content-Disposition", "form-data; name=\"" + parameter.getName() + "\""),
                        RequestBody.create(TEXT, parameter.getValue().getBytes("UTF-8")));
            }/*from   w w w .  j  av a  2  s  .  c o m*/
        }
        return multipartBuilder.build();
    } else {
        return RequestBody.create(FORM_URL_ENCODED,
                HttpParameter.encodeParameters(req.getParameters()).getBytes("UTF-8"));
    }
}