Example usage for com.squareup.okhttp MultipartBuilder MultipartBuilder

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

Introduction

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

Prototype

public MultipartBuilder(String boundary) 

Source Link

Document

Creates a new multipart builder that uses boundary to separate parts.

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  ava 2s . co m*/
        }
        return multipartBuilder.build();
    } else {
        return RequestBody.create(FORM_URL_ENCODED,
                HttpParameter.encodeParameters(req.getParameters()).getBytes("UTF-8"));
    }
}