Example usage for com.squareup.okhttp MediaType parse

List of usage examples for com.squareup.okhttp MediaType parse

Introduction

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

Prototype

public static MediaType parse(String string) 

Source Link

Document

Returns a media type for string , or null if string is not a well-formed media type.

Usage

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

License:Apache License

public void addStringPostParam(String data) {
    MediaType mediaType = MediaType.parse("text/plain");
    requestBuilder.post(RequestBody.create(mediaType, data));
}

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

License:Apache License

public void addFilePostParam(String path) {
    MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=UTF-8");
    requestBuilder.post(RequestBody.create(mediaType, new File(path)));
}

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);//from w w  w . ja  v a  2s . c  om
}

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);/*ww  w.j  a v  a2 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   ww  w.  ja v  a 2s.  c  om
        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.kubeiwu.easyandroid.easyhttp.core.retrofit.KOkHttpCall.java

License:Apache License

private Response<T> execCacheRequest(Request request) {
    if (responseConverter instanceof GsonConverter) {
        GsonConverter<T> converter = (GsonConverter<T>) responseConverter;
        Cache cache = converter.getCache();
        if (cache == null) {
            return null;
        }// w ww  . j a  v a 2 s .  c o m
        Entry entry = cache.get(request.urlString());// ?entry
        if (entry == null) {
            return null;
        }
        if (entry.isExpired()) {// 
            return null;
        }
        if (entry.data != null) {// ?
            MediaType contentType = MediaType.parse(entry.mimeType);
            byte[] bytes = entry.data;
            try {
                com.squareup.okhttp.Response rawResponse = new com.squareup.okhttp.Response.Builder()//
                        .code(200).request(request).protocol(Protocol.HTTP_1_1)
                        .body(ResponseBody.create(contentType, bytes)).build();
                return parseResponse(rawResponse, request);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

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());
            }//  ww  w.  j a  va  2 s  .c o  m

            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 ava  2  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));
        }/*from  ww  w. j  a  v  a2 s  .c  o m*/
    } else if (method == Method.HEAD) {
        builder.head();
    }

    return send(builder, request);
}