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.blackcat.coach.net.OkHttpStack.java

License:Open Source License

@SuppressWarnings("deprecation")
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.getPostBody();
        if (postBody != null) {
            builder.post(RequestBody.create(MediaType.parse(request.getBodyContentType()), postBody));
        }/*  w ww . j a v  a2s. c  o  m*/
        break;
    case Request.Method.GET:
        builder.get();
        break;
    case Request.Method.DELETE:
        builder.delete();
        break;
    case Request.Method.POST:
        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.blackcat.coach.net.OkHttpStack.java

License:Open Source License

private static RequestBody createRequestBody(Request<?> r) throws AuthFailureError {
    final byte[] body = r.getBody();
    if (body == null)
        return null;

    return RequestBody.create(MediaType.parse(r.getBodyContentType()), body);
}

From source file:com.brq.wallet.bitid.BitIdAuthenticator.java

License:Microsoft Reference Source License

protected Request getRequest(SignedMessage signature) {
    MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
    String jsonString = request.getCallbackJson(address, signature).toString();
    RequestBody body = RequestBody.create(jsonType, jsonString);
    String url = request.getCallbackUri();
    return new Request.Builder().url(url).post(body).build();
}

From source file:com.cinchapi.concourse.http.HttpTest.java

License:Apache License

/**
 * Perform a POST request.//from w  w  w. j  a v  a 2 s.  c om
 * 
 * @param route
 * @param data
 * @param args - include a {@link Headers} object to set the request headers
 * @return the response
 */
protected Response post(String route, String data, Object... args) {
    try {
        RequestBody body = RequestBody.create(JSON, data);
        Request.Builder builder = new Request.Builder();
        args = filterArgs(builder, args);
        args = cleanUrlArgs(args);
        route = MessageFormat.format(route, args);
        Request request = builder.url(base + route).post(body).build();
        Response response = http.newCall(request).execute();
        long ts = response.hashCode();
        Variables.register("request_" + ts, request);
        Variables.register("response_" + ts, response);
        return response;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.cinchapi.concourse.http.HttpTest.java

License:Apache License

/**
 * Perform a PUT request./*w ww.  ja v  a  2  s. c  o  m*/
 * 
 * @param route
 * @param data
 * @param args - include a {@link Headers} object to set the request headers
 * @return the response
 */
protected Response put(String route, String data, Object... args) {
    try {
        RequestBody body = RequestBody.create(JSON, data);
        Request.Builder builder = new Request.Builder();
        args = filterArgs(builder, args);
        args = cleanUrlArgs(args);
        route = MessageFormat.format(route, args);
        Request request = builder.url(base + route).put(body).build();
        Response response = http.newCall(request).execute();
        long ts = response.hashCode();
        Variables.register("request_" + ts, request);
        Variables.register("response_" + ts, response);
        return response;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.cliff.hsj.api.common.GsonConverter.java

License:Apache License

@Override
public RequestBody toBody(T value) {
    Buffer buffer = new Buffer();
    Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
    try {//from w w  w . ja  va2s  .  c  o m
        typeAdapter.toJson(writer, value);
        writer.flush();
    } catch (IOException e) {
        throw new AssertionError(e); // Writing to Buffer does no I/O.
    }
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}

From source file:com.commonsware.android.backup.BackupService.java

License:Apache License

private void uploadBackup(File backup) throws IOException {
    Request request = new Request.Builder().url(URL_CREATE_BACKUP).post(RequestBody.create(JSON, "{}")).build();
    Response response = OKHTTP_CLIENT.newCall(request).execute();

    if (response.code() == 201) {
        String backupURL = response.header("Location");

        request = new Request.Builder().url(backupURL + RESOURCE_DATASET).put(RequestBody.create(ZIP, backup))
                .build();//  w  w  w .  j a  v  a 2  s  .co  m
        response = OKHTTP_CLIENT.newCall(request).execute();

        if (response.code() == 201) {
            String datasetURL = response.header("Location");
            SharedPreferences prefs = getSharedPreferences(getClass().getName(), Context.MODE_PRIVATE);

            prefs.edit().putString(PREF_LAST_BACKUP_DATASET, datasetURL).commit();
        } else {
            Log.e(getClass().getSimpleName(), "Unsuccessful request to upload backup");
        }
    } else {
        Log.e(getClass().getSimpleName(), "Unsuccessful request to create backup");
    }
}

From source file:com.cy.retrofithttpstest.retrofit.ToStringConverterFactory.java

License:Apache License

@Override
public Converter<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
    if (String.class.equals(type)) {
        return new Converter<String, RequestBody>() {
            @Override//from www.  jav  a 2  s .  c  o m
            public RequestBody convert(String value) throws IOException {
                return RequestBody.create(MEDIA_TYPE, value);
            }
        };
    }
    return null;
}

From source file:com.datastore_android_sdk.okhttp.OkHttpStack.java

License:Open Source License

private static RequestBody createRequestBody(Request r) {
    final byte[] body = r.getBody();
    if (body == null)
        return null;

    return RequestBody.create(MediaType.parse(r.getBodyContentType()), body);
}

From source file:com.digi.wva.internal.HttpClient.java

License:Mozilla Public License

/**
 * Converts a JSONObject, to be used as the body of a request, to its corresponding
 * {@link RequestBody} representation.//w  w w . j av  a 2s  . co  m
 *
 * <p>Used by {@link #post} and {@link #put}, as well as their synchronous variants.</p>
 *
 * <p>This method is protected, rather than private, due to a bug between JaCoCo and
 * the Android build tools which causes the instrumented bytecode to be invalid when this
 * method is private:
 * <a href="http://stackoverflow.com/questions/17603192/dalvik-transformation-using-wrong-invoke-opcode" target="_blank">see StackOverflow question.</a>
 * </p>
 *
 * @param obj the JSON data to be used
 * @return the request body
 */
protected RequestBody makeBody(JSONObject obj) {
    return obj == null ? null : RequestBody.create(JSON, obj.toString());
}