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:lumbermill.internal.elasticsearch.ElasticSearchOkHttpClientImpl.java

License:Apache License

protected void doOkHttpPost(RequestContext requestCtx) {
    RequestBody body = RequestBody.create(TEXT, requestCtx.signableRequest.payload().get());
    Request request = new Request.Builder().url(url).post(body)
            .headers(Headers.of(requestCtx.signableRequest.headers())).build();

    // Add some sanity logging to be able to figure out the load
    if (LOGGER.isDebugEnabled()) {
        int requestsInQueue = client.getDispatcher().getQueuedCallCount();
        int requestsInProgress = client.getDispatcher().getRunningCallCount();
        if (requestsInQueue > 0) {
            LOGGER.debug("There are {} requests waiting to be processed", requestsInQueue);
        }/*  w ww  . j  a  v a  2  s .c o m*/
        if (requestsInProgress > 0) {
            LOGGER.debug("There are {} requests currently executing", requestsInProgress);
        }
    }

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Request request, IOException e) {
            requestCtx.error(IndexFailedException.ofIOException(e));
        }

        @Override
        public void onResponse(Response response) throws IOException {
            handleResponse(requestCtx, response);
        }
    });
}

From source file:meteor.operations.Meteor.java

License:Apache License

/**
 * Sends a string over the websocket/*from w ww  .ja va2s  . c o  m*/
 *
 * @param message the string to send
 */
private void send(String callId, final String message) {
    if (message == null) {
        throw new RuntimeException("You cannot send `null` messages");
    }

    try {
        Timber.d("-->" + message);
        synchronized (mConnection) {
            RequestBody request = RequestBody.create(WebSocket.TEXT, message);
            mConnection.sendMessage(request);
        }
    } catch (Exception e) {
        final Listener listener = mListeners.remove(callId);
        if (listener != null) {
            if (listener instanceof ResultListener) {
                ((ResultListener) listener).onError(new MeteorException(e));
            }
        }
        if (mCallback != null) {
            mCallback.onException(e);
        }
    }
}

From source file:microsoft.aspnet.signalr.client.http.android.AndroidOkHttpConnection.java

License:Open Source License

private static com.squareup.okhttp.Request createRequest(Request request) {
    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());
    switch (request.getVerb()) {
    case Constants.HTTP_GET:
        okHttpRequestBuilder.get();//from  w ww  .  ja  v  a 2 s  .  c o  m
        break;
    case Constants.HTTP_POST:
        okHttpRequestBuilder.post(RequestBody.create(null, request.getContent()));
        break;
    default:
        throw new IllegalArgumentException(String.format("%s is not GET or POST", request.getVerb()));
    }

    for (String key : request.getHeaders().keySet()) {
        okHttpRequestBuilder.addHeader(key, request.getHeaders().get(key));
    }

    return okHttpRequestBuilder.build();
}

From source file:net.codestory.rest.misc.PostBody.java

License:Apache License

public static RequestBody json(String body) {
    return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body);
}

From source file:net.money2013.win.hb.net.utils.RestClient.java

public String sendSyncData(String userName, String password, String data) throws IOException {
    RequestBody requestBody = RequestBody.create(TEXT_HTML, data);
    Request request = new Request.Builder().url(BASE_URL + "/api/v1/sync.json")
            .addHeader(AUTHORIZATION_HEADER, createAuthenticationHeader(userName, password)).post(requestBody)
            .build();//from   w  w  w.j  a  v a2s  .  c  o m

    Response response = this.newCall(request).execute();

    return response.body().string();
}

From source file:net.omnypay.sdk.exampleapp.network.HttpOps.java

License:Apache License

public static void doPost(final String url, final Object postBody, final Listener callback) {
    new Thread(new Runnable() {
        @Override/* w w w .  ja  v a2 s  .  c om*/
        public void run() {
            Gson gson = new Gson();
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(JSON, gson.toJson(postBody));
            String timestamp = "" + ((new Date().getTime()) / 1000);
            String localVarPath = "/api/identity/authentication".replaceAll("\\{format\\}", "json");
            String signature = generateSignature("POST", timestamp, Constants.API_KEY, Constants.API_SECRET,
                    localVarPath, Constants.CORRELATION_ID, postBody);
            Request request = new Request.Builder().header("X-api-key", Constants.API_KEY)
                    .header("X-correlation-id", Constants.CORRELATION_ID).header("X-timestamp", timestamp)
                    .header("X-signature", signature).url(url).post(body).build();
            try {
                Response response = client.newCall(request).execute();
                callback.onResult(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }).start();
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

protected RequestBody createMultipartBody(StrParam[] stringStrParams, IOParam[] IOParams) {
    MultipartBuilder builder = new MultipartBuilder();
    builder.type(MultipartBuilder.FORM);
    builder = buildMultipartBody(builder);

    if (stringStrParams != null && stringStrParams.length > 0) {
        for (StrParam strParam : stringStrParams) {
            if (strParam.key != null && strParam.value != null) {
                builder.addFormDataPart(strParam.key, strParam.value);
                log("buildMultiStringParam: key: " + strParam.key + " value: " + strParam.value);
            } else {
                log("buildMultiStringParam: key: " + (strParam.key != null ? strParam.key : "null") + " value: "
                        + (strParam.value != null ? strParam.value : "null"));
            }/* w  w  w  . ja va 2s .co m*/
        }
    }

    if (IOParams != null && IOParams.length > 0) {
        for (IOParam param : IOParams) {
            if (param.key != null && param.file != null) {
                String fileName = param.file.getName();
                RequestBody fileBody = RequestBody.create(MediaType.parse(Util.getFileMimeType(fileName)),
                        param.file);
                builder.addFormDataPart(param.key, fileName, fileBody);
                log("buildMultiFileParam: key: " + param.key + " value: " + fileName);
            } else {
                log("buildMultiFileParam: key: " + (param.key != null ? param.key : "null") + " file: "
                        + (param.file != null ? param.file.getName() : "null"));
            }
        }
    }
    return builder.build();
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, String string) {
    RequestBody body = RequestBody
            .create(MediaType.parse(String.format("text/plain; charset=%s", mProtocolCharset)), string);

    return builderPost(url, body);
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, byte[] bytes) {
    RequestBody body = RequestBody.create(
            MediaType.parse(String.format("application/octet-stream; charset=%s", mProtocolCharset)), bytes);

    return builderPost(url, body);
}

From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java

License:Apache License

@Override
public Request.Builder builderPost(String url, File file) {
    RequestBody body = RequestBody.create(
            MediaType.parse(String.format("application/octet-stream; charset=%s", mProtocolCharset)), file);

    return builderPost(url, body);
}