Example usage for com.squareup.okhttp RequestBody create

List of usage examples for com.squareup.okhttp RequestBody create

Introduction

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

Prototype

public static RequestBody create(final MediaType contentType, final File file) 

Source Link

Document

Returns a new request body that transmits the content of file .

Usage

From source file:com.kkbox.toolkit.internal.api.APIRequest.java

License:Apache License

public void addByteArrayPostParam(final byte[] data) {
    MediaType mediaType = MediaType.parse("application/octet-stream");
    RequestBody requestBody = RequestBody.create(mediaType, data);
    requestBuilder.post(requestBody);/*w  w  w . j  a v a2  s  . com*/
}

From source file:com.kkbox.toolkit.internal.api.APIRequest.java

License:Apache License

public void addJSONPostParam(JSONObject jsonObject) {
    MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
    RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString());
    requestBuilder.post(requestBody);/*from   w w  w  .  ja va  2  s  . co m*/
}

From source file:com.kubeiwu.commontool.khttp.toolbox.OkHttpStack.java

License:Open Source License

private static void setConnectionParametersForRequest(com.squareup.okhttp.Request.Builder builder,
        Request<?> request) throws IOException, AuthFailureError {
    switch (request.getMethod()) {
    case Request.Method.DEPRECATED_GET_OR_POST:
        // Ensure backwards compatibility.  Volley assumes a request with a null body is a GET.
        byte[] postBody = request.getBody();
        if (postBody != null) {
            builder.post(RequestBody.create(MediaType.parse(request.getBodyContentType()), postBody));
        }//from www.j  ava 2s  .c o m
        break;
    case Request.Method.GET:
        builder.get();
        break;
    case Request.Method.DELETE:
        builder.delete();
        break;
    case Request.Method.POST:
        //                builder.get();
        builder.post(createRequestBody(request));
        break;
    case Request.Method.PUT:
        builder.put(createRequestBody(request));
        break;
    //            case Request.Method.HEAD:
    //                builder.head();
    //                break;
    //            case Request.Method.OPTIONS:
    //                builder.method("OPTIONS", null);
    //                break;
    //            case Request.Method.TRACE:
    //                builder.method("TRACE", null);
    //                break;
    //            case Request.Method.PATCH:
    //                builder.patch(createRequestBody(request));
    //                break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

From source file:com.lidroid.xutils.HttpUtils.java

License:Apache License

@NonNull
private RequestBody buildFormRequestBody(RequestParams params) {

    if (params != null) {
        List<NameValuePair> bodyParams = params.getBodyParams();

        if (bodyParams != null) {
            FormEncodingBuilder builder = new FormEncodingBuilder();

            for (NameValuePair param : params.getBodyParams()) {
                builder.add(param.getName(), param.getValue());
            }/*from  w  w  w.  ja v a 2s .  c  om*/

            return builder.build();
        }

        HttpEntity entity = params.getEntity();
        if (entity != null) {
            try {
                String body = InputStreamToString(entity.getContent(), "utf-8");
                return RequestBody.create(MediaType.parse("application/javascript"), body);
            } catch (Exception ex) {
                LogUtil.e(ex.getMessage());
            }
        }
    }

    return RequestBody.create(MediaType.parse("application/javascript"), "");
}

From source file:com.lidroid.xutils.HttpUtils.java

License:Apache License

@NonNull
private RequestBody buildFileRequestBody(RequestParams params, HashMap<String, ContentBody> fileParams) {
    MultipartBuilder builder = new MultipartBuilder();

    builder = builder.type(MultipartBuilder.FORM);
    for (String fileName : fileParams.keySet()) {
        builder.addFormDataPart(fileName, fileName, RequestBody.create(
                MediaType.parse("application/octet-stream"), ((FileBody) fileParams.get(fileName)).getFile()));
    }/* w w  w .  j a  v a2  s. co m*/

    List<NameValuePair> bodyParams = params.getBodyParams();

    if (bodyParams != null) {
        for (NameValuePair param : params.getBodyParams()) {
            builder.addFormDataPart(param.getName(), param.getValue());
        }
    }

    return builder.build();
}

From source file:com.liferay.mobile.android.auth.CookieSignIn.java

License:Open Source License

protected Builder getBuilder(Session session, String username, String password) throws IOException {

    Builder builder = new Builder();

    MediaType contentType = MediaType.parse("application/x-www-form-urlencoded");

    builder.post(RequestBody.create(contentType, getBody(username, password)));

    builder.addHeader("Cookie", "COOKIE_SUPPORT=true;");
    builder.url(getLoginURL(session.getServer()));

    return builder;
}

From source file:com.liferay.mobile.android.http.client.OkHttpClientImpl.java

License:Open Source License

@Override
public Response send(Request request) throws Exception {
    Builder builder = new Builder();
    Method method = request.getMethod();

    if (method == Method.POST) {
        String body = (String) request.getBody();

        if (body != null) {
            MediaType type = MediaType.parse("application/json; charset=utf-8");

            builder.post(RequestBody.create(type, body));
        }/*w ww  .  j a v  a2 s.c o m*/
    } else if (method == Method.HEAD) {
        builder.head();
    }

    return send(builder, request);
}

From source file:com.liferay.mobile.sdk.auth.CookieSignIn.java

License:Open Source License

public static void signIn(Config config, CookieCallback callback) {
    try {//from  w  w w .j  a va  2s.  c o m
        Authentication auth = config.auth();

        if (!(auth instanceof BasicAuthentication)) {
            throw new Exception(
                    "Can't sign in if authentication implementation is not " + "BasicAuthentication");
        }

        OkHttpClient client = new OkHttpClient();

        CookieManager cookieManager = new CookieManager();

        cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

        client.setCookieHandler(cookieManager);
        client.setFollowRedirects(true);

        Builder builder = new Builder();

        MediaType contentType = MediaType.parse("application/x-www-form-urlencoded");

        builder.post(RequestBody.create(contentType, getBody((BasicAuthentication) auth)));

        builder.addHeader("Cookie", "COOKIE_SUPPORT=true;");
        builder.url(getLoginURL(config.server()));

        Call call = client.newCall(builder.build());

        call.enqueue(getCallback(callback, cookieManager));
    } catch (Exception e) {
        callback.onFailure(e);
    }
}

From source file:com.liuguangqiang.asyncokhttp.AsyncOkHttp.java

License:Apache License

/**
 * Perform HTTP POST request with a JSON string.
 *
 * @param url             the URL of HTTP request.
 * @param json            the parameter of Request Body.
 * @param responseHandler the callback of the response.
 *//*from ww w  .  jav  a2  s. com*/
public void post(String url, String json, BaseResponseHandler responseHandler) {
    RequestBody requestBody = RequestBody.create(JSON, json);
    post(url, requestBody, responseHandler);
}

From source file:com.liuguangqiang.asyncokhttp.AsyncOkHttp.java

License:Apache License

/**
 * Perform HTTP PUT request with a JSON string.
 *
 * @param url             the URL of HTTP request.
 * @param json            the parameter of Request Body.
 * @param responseHandler the callback of the response.
 *///  w  w  w .  j  av  a2  s .  co  m
public void put(String url, String json, BaseResponseHandler responseHandler) {
    RequestBody requestBody = RequestBody.create(JSON, json);
    put(url, requestBody, responseHandler);
}