List of usage examples for com.squareup.okhttp MediaType parse
public static MediaType parse(String string)
From source file:cc.arduino.mvd.services.HttpService.java
License:Apache License
/** * Perform a POST request to the service * * @param url//from www . j av a 2s .com * @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:cc.arduino.mvd.services.HttpService.java
License:Apache License
/** * Get something from the service/*from www . j a v a 2s. co m*/ * * @param url * @return * @throws IOException */ private void get(String url) throws IOException { MediaType JSON = MediaType.parse("application/json; charset=utf-8"); final Request request = new Request.Builder().url(url).get().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 { try { String body = new String(response.body().bytes()); JSONObject json = new JSONObject(body); handleKeyValFromHttp(json.getString("code"), json.getString("pin"), json.getString("value")); } catch (JSONException e) { e.printStackTrace(); } } }); }
From source file:client.ui.Container.java
private Response request(OkHttpClient client, URL url, String json) { Response response = null;//from w w w . j a v a 2 s . co 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
public void put(String key, File file, String contentType) { if (file == null || !file.exists() || file.length() == 0) { return;//w w w.j ava2 s . co m } MediaType mediaType = null; try { mediaType = MediaType.parse(contentType); } catch (Exception e) { Logger.e(e); } put(key, new FileWrapper(file, mediaType)); }
From source file:cn.finalteam.okhttpfinal.RequestParams.java
License:Apache License
protected RequestBody getRequestBody() { RequestBody body = null;// w w w. j a v a 2 s . c o m 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);// w w w . j a v a 2 s. com 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:cn.wochu.wh.net.OkHttpClientManager.java
License:Apache License
@SuppressWarnings("static-access") private Request buildPostRequest(String url, Param[] params) { if (params == null) { params = new Param[0]; }// w w w. j a va2s . co m FormEncodingBuilder builder = new FormEncodingBuilder(); for (Param param : params) { builder.add(param.key, param.value); } RequestBody requestBody = builder.build().create(MediaType.parse("application/json; charset=utf-8"), "json"); return new Request.Builder().url(url).post(requestBody).build(); }
From source file:cn.wochu.wh.net.OkHttpClientManager.java
License:Apache License
private Request buildPostRequest(String url, String params) { FormEncodingBuilder builder = new FormEncodingBuilder(); RequestBody requestBody = builder.build().create(MediaType.parse("application/json; charset=utf-8"), params);/*w w w.j a v a2s .c o m*/ return new Request.Builder().url(url).post(requestBody).build(); }
From source file:cn.wochu.wh.net.OkHttpClientManager.java
License:Apache License
private Request buildPostRequest(String url, Param[] params, String content) { if (params == null) { params = new Param[0]; }/*from w ww .j a v a2 s . co m*/ FormEncodingBuilder builder = new FormEncodingBuilder(); for (Param param : params) { builder.add(param.key, param.value); } RequestBody requestBody = builder.build().create(MediaType.parse("application/json; charset=utf-8"), content); 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 w w w. ja v a2 s . c o m*/ new Request.Builder().method("GET", RequestBody.create(MediaType.parse("text/plain"), "abc")); fail(); } catch (IllegalArgumentException expected) { } }