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.android.cloudfiles.cloudfilesforandroid.CountingFileRequestBody.java

License:Apache License

@Override
public MediaType contentType() {
    return MediaType.parse(contentType);
}

From source file:com.apothesource.pillfill.service.prescription.impl.DefaultPrescriptionServiceImpl.java

License:Open Source License

@Override
public Observable<AccountAggregationTaskResponse> requestPrescriptionExtraction(
        AccountAggregationTaskRequest request) {
    return subscribeIoObserveImmediate(subscriber -> {
        String responseStr = null;

        try {//from w  w  w . j  a  v  a  2 s .  co  m
            OkHttpClient client = PFNetworkManager.getPinnedPFHttpClient();
            Request req = new Request.Builder()
                    .post(RequestBody.create(MediaType.parse(HTTP_CONTENT_TYPE_JSON), gson.toJson(request)))
                    .url(PFServiceEndpoints.RX_REQUEST_EXTRACT_URL).build();

            Response res = client.newCall(req).execute();
            responseStr = res.body().string();
            AccountAggregationTaskResponse response = gson.fromJson(responseStr,
                    AccountAggregationTaskResponse.class);
            subscriber.onNext(response);
            subscriber.onCompleted();
        } catch (IOException e) {
            log.log(Level.SEVERE, "Communication error during extraction request.");
            subscriber.onError(e);
        } catch (JsonSyntaxException e) {
            log.severe(String.format("Error parsing response: %s", responseStr));
            subscriber.onError(e);
        }
    });
}

From source file:com.apothesource.pillfill.service.prescription.impl.DefaultPrescriptionServiceImpl.java

License:Open Source License

@Override
public Observable<PrescriptionType> enrichPrescriptions(List<PrescriptionType> rxList) {
    return subscribeIoObserveImmediate(subscriber -> {
        try {/* www .j  av a2s  . co m*/
            OkHttpClient client = PFNetworkManager.getPinnedPFHttpClient();
            Request req = new Request.Builder()
                    .post(RequestBody.create(MediaType.parse(HTTP_CONTENT_TYPE_JSON), gson.toJson(rxList)))
                    .url(PFServiceEndpoints.RX_ENRICH_URL).build();
            Response res = client.newCall(req).execute();
            List<PrescriptionType> enrichedRxList = gson.fromJson(res.body().charStream(), RX_LIST_TYPE);
            Observable.from(enrichedRxList).forEach(subscriber::onNext);
            subscriber.onCompleted();
        } catch (IOException ex) {
            Logger.getLogger(DefaultPrescriptionServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            subscriber.onError(ex);
        }
    });
}

From source file:com.apothesource.pillfill.service.reminder.impl.DefaultReminderServiceImpl.java

License:Open Source License

@Override
public Observable<ReminderWSResponse> updateRemindersForRx(List<Reminder> reminderList, PrescriptionType rx) {
    return subscribeIoObserveImmediate(subscriber -> {
        try {/*from   w  w  w .  j av  a  2 s. c  o  m*/
            RequestBody reminderJson = RequestBody.create(MediaType.parse("application/json"),
                    mGson.toJson(reminderList));
            String url = String.format(PFServiceEndpoints.REMINDER_BASE_URL, rx.getUuid());
            Request.Builder builder = new Request.Builder().url(url).post(reminderJson);
            Response response = mHttpClient.newCall(builder.build()).execute();
            String responseJson = response.body().string();
            ReminderWSResponse wsResponse = mGson.fromJson(responseJson, ReminderWSResponse.class);
            subscriber.onNext(wsResponse);
            subscriber.onCompleted();
        } catch (IOException e) {
            mLog.log(Level.SEVERE, "Failed to update reminders.", e);
            subscriber.onError(e);
        }
    });
}

From source file:com.askfast.askfastapi.util.HttpUtil.java

License:Apache License

/**
 * Send a post request/*from w  w  w. j a  v a 2  s.  c  om*/
 * 
 * @param url
 *            Url as string
 * @param body
 *            Request body as string
 * @param headers
 *            Optional map with headers
 * @return response Response as string
 * @throws IOException
 *             Errors in connecting to the given URL
 */
static public String post(String url, String body, Map<String, String> headers) throws IOException {

    MediaType mediaType = MediaType.parse("application/json");
    RequestBody requestBody = RequestBody.create(mediaType, body);
    HttpUtil httpUtil = new HttpUtil();
    Request request = httpUtil.getBuilderWIthHeaders(url, headers).post(requestBody).build();
    httpUtil.response = httpUtil.client.newCall(request).execute();
    return httpUtil.response.body().string();
}

From source file:com.askfast.askfastapi.util.HttpUtil.java

License:Apache License

/**
 * Send a put request/*from  www.  j  a  v a 2  s  .c  om*/
 * 
 * @param url
 *            Url as string
 * @param body
 *            Request body as string
 * @param headers
 *            Optional map with headers
 * @return response Response as string
 * @throws IOException
 *             Errors in connecting to the given URL
 */
static public String put(String url, String body, Map<String, String> headers) throws IOException {

    MediaType mediaType = MediaType.parse("application/json");
    RequestBody requestBody = RequestBody.create(mediaType, body);
    HttpUtil httpUtil = new HttpUtil();
    Request request = httpUtil.getBuilderWIthHeaders(url, null).put(requestBody).build();
    httpUtil.response = httpUtil.client.newCall(request).execute();
    return httpUtil.response.body().string();
}

From source file:com.baasbox.android.net.OkClient.java

License:Apache License

private RequestBody buildBody(String contentType, InputStream bodyData) {
    if (bodyData == null) {
        return RequestBody.create(MediaType.parse("application/json;charset=" + charset), "{}");
    } else {/*from  www .j a  va2 s. co  m*/
        return new InputRequestBody(contentType, bodyData);
    }
}

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));
        }/*from www .j  ava2  s .  c om*/
        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();
}