List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
private RequestBody createEmptyRequestBody() { return RequestBody.create(JSON, EMPTY_JSON); }
From source file:br.org.cesar.knot.lib.connection.ThingApi.java
License:Open Source License
private RequestBody createRequestBodyWith(String data) { return RequestBody.create(JSON, data); }
From source file:cc.arduino.mvd.services.HttpService.java
License:Apache License
/** * Perform a POST request to the service * * @param url/*from w ww.j a v a 2 s . c o m*/ * @param json * @return * @throws IOException */ private void post(String url, String json) { MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, json); Log.d(TAG, "POST: " + url); final Request request = new Request.Builder().url(url).post(body).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(final Request request, final IOException exception) { // Meh, don't do anything... exception.printStackTrace(); } @Override public void onResponse(final Response response) throws IOException { // Meh, don't do anything... Log.d(TAG, request.toString()); } }); }
From source file:client.ui.Container.java
private Response request(OkHttpClient client, URL url, String json) { Response response = null;/*from w ww . jav a 2 s . c o m*/ try { log("Requesting: " + client.getProtocols().get(0) + " => " + url.toString()); Builder builder = new Request.Builder().url(url.toString()); if (!"".equals(json)) { builder.post(RequestBody.create(MediaType.parse("application/json"), json)); } Request request = builder.build(); response = client.newCall(request).execute(); log("Completed: " + response.code()); } catch (ConnectException e) { log("\n" + "Failed: " + e.getMessage()); } catch (IOException e) { log("Failed: " + e.getMessage()); } return response; }
From source file:cn.finalteam.okhttpfinal.RequestParams.java
License:Apache License
protected RequestBody getRequestBody() { RequestBody body = null;//w ww .jav a 2s .com if (jsonBody != null) { body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonBody.toJSONString()); } else if (requestBody != null) { body = requestBody; } else if (fileParams.size() > 0) { boolean hasData = false; MultipartBuilder builder = new MultipartBuilder(); builder.type(MultipartBuilder.FORM); for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) { builder.addFormDataPart(entry.getKey(), entry.getValue()); hasData = true; } for (ConcurrentHashMap.Entry<String, FileWrapper> entry : fileParams.entrySet()) { FileWrapper file = entry.getValue(); if (file != null) { hasData = true; builder.addFormDataPart(entry.getKey(), file.getFileName(), RequestBody.create(file.getMediaType(), file.getFile())); } } if (hasData) { body = builder.build(); } } else { FormEncodingBuilder builder = new FormEncodingBuilder(); boolean hasData = false; for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) { builder.add(entry.getKey(), entry.getValue()); hasData = true; } if (hasData) { body = builder.build(); } } return body; }
From source file:cn.wochu.wh.net.OkHttpClientManager.java
License:Apache License
private Request buildMultipartFormRequest(String url, File[] files, String[] fileKeys, Param[] params) { params = validateParam(params);// ww w . java 2 s .co m MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM); for (Param param : params) { builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + param.key + "\""), RequestBody.create(null, param.value)); } if (files != null) { RequestBody fileBody = null; for (int i = 0; i < files.length; i++) { File file = files[i]; String fileName = file.getName(); fileBody = RequestBody.create(MediaType.parse(guessMimeType(fileName)), file); //TODO ???contentType builder.addPart( Headers.of("Content-Disposition", "form-data; name=\"" + fileKeys[i] + "\"; filename=\"" + fileName + "\""), fileBody); } } RequestBody requestBody = builder.build(); return new Request.Builder().url(url).post(requestBody).build(); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void getWithRequestBody() throws Exception { server.enqueue(new MockResponse()); try {//from ww w .j a v a2s . c om new Request.Builder().method("GET", RequestBody.create(MediaType.parse("text/plain"), "abc")); fail(); } catch (IllegalArgumentException expected) { } }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void post() throws Exception { server.enqueue(new MockResponse().setBody("abc")); Request request = new Request.Builder().url(server.getUrl("/")) .post(RequestBody.create(MediaType.parse("text/plain"), "def")).build(); FiberOkHttpTestUtil.executeInFiberRecorded(client, request).assertCode(200).assertBody("abc"); RecordedRequest recordedRequest = server.takeRequest(); assertEquals("POST", recordedRequest.getMethod()); assertEquals("def", recordedRequest.getBody().readUtf8()); assertEquals("3", recordedRequest.getHeader("Content-Length")); assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type")); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
@Test public void postZeroLength() throws Exception { server.enqueue(new MockResponse().setBody("abc")); Request request = new Request.Builder().url(server.getUrl("/")) .method("POST", RequestBody.create(null, new byte[0])).build(); FiberOkHttpTestUtil.executeInFiberRecorded(client, request).assertCode(200).assertBody("abc"); RecordedRequest recordedRequest = server.takeRequest(); assertEquals("POST", recordedRequest.getMethod()); assertEquals(0, recordedRequest.getBody().size()); assertEquals("0", recordedRequest.getHeader("Content-Length")); assertEquals(null, recordedRequest.getHeader("Content-Type")); }
From source file:co.paralleluniverse.fibers.okhttp.CallTest.java
License:Open Source License
private void postBodyRetransmittedAfterAuthorizationFail(String body) throws Exception { server.enqueue(new MockResponse().setResponseCode(401)); server.enqueue(new MockResponse()); Request request = new Request.Builder().url(server.getUrl("/")) .method("POST", RequestBody.create(null, body)).build(); String credential = Credentials.basic("jesse", "secret"); client.setAuthenticator(new RecordingOkAuthenticator(credential)); Response response = FiberOkHttpTestUtil.executeInFiberRecorded(client, request).response; assertEquals(200, response.code());/*w w w. j a v a 2s.c o m*/ RecordedRequest recordedRequest1 = server.takeRequest(); assertEquals("POST", recordedRequest1.getMethod()); assertEquals(body, recordedRequest1.getBody().readUtf8()); assertNull(recordedRequest1.getHeader("Authorization")); RecordedRequest recordedRequest2 = server.takeRequest(); assertEquals("POST", recordedRequest2.getMethod()); assertEquals(body, recordedRequest2.getBody().readUtf8()); assertEquals(credential, recordedRequest2.getHeader("Authorization")); }