List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:com.anony.okhttp.sample.PostString.java
License:Apache License
public void run() throws Exception { String postBody = "" + "Releases\n" + "--------\n" + "\n" + " * _1.0_ May 6, 2013\n" + " * _1.1_ June 15, 2013\n" + " * _1.2_ August 11, 2013\n"; Request request = new Request.Builder().url("https://api.github.com/markdown/raw") .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody)).build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
From source file:com.anony.okhttp.sample.RequestBodyCompression.java
License:Apache License
public void run() throws Exception { Map<String, String> requestBody = new LinkedHashMap<>(); requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/"); RequestBody jsonRequestBody = RequestBody.create(MEDIA_TYPE_JSON, new Gson().toJson(requestBody)); Request request = new Request.Builder() .url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY).post(jsonRequestBody) .build();/*from w w w . ja v a 2 s .co m*/ Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); }
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 ww.j av a2s .c o 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 {/*from www. j a v a2 s . com*/ 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 {// w w w .ja va 2 s .com 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 .com * * @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 w w w . j a v a 2 s. c o m * * @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.asynhkm.productchecker.Checker.CheckerTask.java
@Override protected DataProductVersion doInBackground(Void... c) { DataProductVersion h;/* w w w. ja va 2 s.c o m*/ try { RequestBody body = RequestBody.create(JSON, consolidate()); Request request = new Request.Builder().url(request_url).post(body).build(); Response response = client.newCall(request).execute(); String res = response.body().string(); h = return_result(res); } catch (NoClassDefFoundError e) { h = new DataProductVersion(); h.setRR(new ReturnResult(e.getMessage())); } catch (IOException e) { Log.d("work ERROR", e.getMessage()); h = new DataProductVersion(); h.setRR(new ReturnResult(e.getMessage())); } catch (Exception e) { Log.d("work ERROR", e.getMessage()); h = new DataProductVersion(); h.setRR(new ReturnResult(e.getMessage())); } return h; }
From source file:com.auth0.api.internal.JsonRequestBodyBuilder.java
License:Open Source License
public static RequestBody createBody(Object pojo, ObjectWriter writer) throws RequestBodyBuildException { try {/*w w w.j av a 2 s. c o m*/ return RequestBody.create(JSON, writer.writeValueAsBytes(pojo)); } catch (JsonProcessingException e) { throw new RequestBodyBuildException("Failed to convert " + pojo.getClass().getName() + " to JSON", e); } }
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 {// ww w .j a v a2s . co m return new InputRequestBody(contentType, bodyData); } }