List of usage examples for com.squareup.okhttp RequestBody create
public static RequestBody create(final MediaType contentType, final File file)
From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java
License:Apache License
@Override public Request.Builder builderPost(String url, JSONObject jsonObject) { RequestBody body = RequestBody.create( MediaType.parse(String.format("application/json; charset=%s", mProtocolCharset)), jsonObject.toString());// ww w .j a va2 s . c om return builderPost(url, body); }
From source file:net.qiujuer.common.okhttp.impl.RequestCallBuilder.java
License:Apache License
@Override public Request.Builder builderPost(String url, JSONArray jsonArray) { RequestBody body = RequestBody.create( MediaType.parse(String.format("application/json; charset=%s", mProtocolCharset)), jsonArray.toString());//ww w . j av a2s .co m return builderPost(url, body); }
From source file:net.uiqui.oblivion.client.rest.RestClient.java
License:Apache License
public RestOutput put(final URL url, final String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder().url(url).put(body).build(); Response response = client.newCall(request).execute(); return RestOutput.parse(response); }
From source file:net.uiqui.oblivion.client.rest.RestClient.java
License:Apache License
public RestOutput post(final URL url, final String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder().url(url).post(body).build(); Response response = client.newCall(request).execute(); return RestOutput.parse(response); }
From source file:net.yatomiya.nicherry.services.bbs.PostMessageHandler.java
License:Open Source License
public Call execute(ThreadProcessor processor, boolean isSynchronous, String name, String mail, String body, Map<String, String> additionalParams) { this.processor = processor; this.isSynchronous = isSynchronous; this.name = name; this.mail = mail; this.body = body; executeTime = JUtils.getCurrentTime(); EUtils.get(EventService.class).send(BBSService.Event.Post.START, getModel()); BBSService bbsSrv = processor.getManager().getBBSService(); BBSHttpClient client = bbsSrv.getHttpClient(); ThreadId threadId = getModel().getId(); MBoard board = bbsSrv.getBoard(threadId.getBoardId()); String hostUrl = HttpUtils.getHostUrl(board.getUrl()); String postUrl = hostUrl + "/test/bbs.cgi"; Request.Builder builder = new Request.Builder(); builder.url(postUrl);/*from ww w. java 2 s. c o m*/ Map<String, String> valueMap = new HashMap<>(); valueMap.put("bbs", threadId.getBoardId().getIdValue()); valueMap.put("key", threadId.getIdValue()); valueMap.put("time", String.valueOf((JUtils.getCurrentTime() / 1000) - 60)); valueMap.put("submit", "??"); valueMap.put("FROM", name); valueMap.put("mail", mail); valueMap.put("MESSAGE", body); if (additionalParams != null) valueMap.putAll(additionalParams); String valueStr = HttpUtils.buildFormPostData(valueMap, NConstants.CHARSET_SHIFT_JIS); builder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=Shift_JIS"), StringUtils.getBytes(valueStr, NConstants.CHARSET_SHIFT_JIS))); builder.header("Referer", board.getUrl()); Request request = builder.build(); Callback callback = new Callback() { @Override public void onResponse(Response response) throws IOException { EUtils.asyncExec(() -> doOnResponse(response)); } @Override public void onFailure(Request request, IOException e) { EUtils.asyncExec(() -> doOnFailure(request, e)); } }; httpCall = client.execute(request, callback, isSynchronous); postEvent = new PostMessageEvent(name, mail, body); return httpCall; }
From source file:nextflow.ga4gh.tes.client.ApiClient.java
License:Apache License
/** * Build an HTTP request with the given options. * * @param path The sub-path of the HTTP URL * @param method The request method, one of "GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH" and "DELETE" * @param queryParams The query parameters * @param body The request body object/*from w w w . jav a 2s. co m*/ * @param headerParams The header parameters * @param formParams The form parameters * @param authNames The authentications to apply * @param progressRequestListener Progress request listener * @return The HTTP request * @throws ApiException If fail to serialize the request body object */ public Request buildRequest(String path, String method, List<Pair> queryParams, Object body, Map<String, String> headerParams, Map<String, Object> formParams, String[] authNames, ProgressRequestBody.ProgressRequestListener progressRequestListener) throws ApiException { updateParamsForAuth(authNames, queryParams, headerParams); final String url = buildUrl(path, queryParams); final Request.Builder reqBuilder = new Request.Builder().url(url); processHeaderParams(headerParams, reqBuilder); String contentType = (String) headerParams.get("Content-Type"); // ensuring a default content type if (contentType == null) { contentType = "application/json"; } RequestBody reqBody; if (!HttpMethod.permitsRequestBody(method)) { reqBody = null; } else if ("application/x-www-form-urlencoded".equals(contentType)) { reqBody = buildRequestBodyFormEncoding(formParams); } else if ("multipart/form-data".equals(contentType)) { reqBody = buildRequestBodyMultipart(formParams); } else if (body == null) { if ("DELETE".equals(method)) { // allow calling DELETE without sending a request body reqBody = null; } else { // use an empty request body (for POST, PUT and PATCH) reqBody = RequestBody.create(MediaType.parse(contentType), ""); } } else { reqBody = serialize(body, contentType); } Request request = null; if (progressRequestListener != null && reqBody != null) { ProgressRequestBody progressRequestBody = new ProgressRequestBody(reqBody, progressRequestListener); request = reqBuilder.method(method, progressRequestBody).build(); } else { request = reqBuilder.method(method, reqBody).build(); } return request; }
From source file:org.addhen.smssync.data.net.AppHttpClient.java
License:Open Source License
/** * Get HTTP Entity populated with data in a format specified by the current sync scheme *//*from ww w . j ava2 s . c om*/ private void setHttpEntity(SyncScheme.SyncDataFormat format) throws Exception { RequestBody body; switch (format) { case JSON: body = RequestBody.create(JSON, DataFormatUtil.makeJSONString(getParams())); log("setHttpEntity format JSON"); break; case XML: body = RequestBody.create(XML, DataFormatUtil.makeXMLString(getParams(), "payload", UTF_8.name())); log("setHttpEntity format XML"); break; case URLEncoded: log("setHttpEntity format URLEncoded"); FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder(); List<HttpNameValuePair> params = getParams(); for (HttpNameValuePair pair : params) { formEncodingBuilder.add(pair.getName(), pair.getValue()); } body = formEncodingBuilder.build(); break; default: throw new Exception("Invalid data format"); } setRequestBody(body); }
From source file:org.addhen.smssync.data.net.MessageHttpClient.java
License:Open Source License
/** * Get HTTP Entity populated with data in a format specified by the current sync scheme *//*from w w w. j ava 2 s.c o m*/ private void setHttpEntity(SyncScheme.SyncDataFormat format) throws Exception { RequestBody body; switch (format) { case JSON: body = RequestBody.create(JSON, DataFormatUtil.makeJSONString(getParams())); log("setHttpEntity format JSON"); mFileManager.appendAndClose("setHttpEntity format JSON"); break; case XML: body = RequestBody.create(XML, DataFormatUtil.makeXMLString(getParams(), "payload", UTF_8.name())); log("setHttpEntity format XML"); mFileManager.appendAndClose(mContext.getString(R.string.http_entity_format, "XML")); break; case URLEncoded: log("setHttpEntity format URLEncoded"); FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder(); List<HttpNameValuePair> params = getParams(); for (HttpNameValuePair pair : params) { formEncodingBuilder.add(pair.getName(), pair.getValue()); } mFileManager.appendAndClose(mContext.getString(R.string.http_entity_format, "URLEncoded")); body = formEncodingBuilder.build(); break; default: mFileManager.appendAndClose(mContext.getString(R.string.invalid_data_format)); throw new Exception("Invalid data format"); } setRequestBody(body); }
From source file:org.addhen.smssync.net.MessageSyncHttpClient.java
License:Open Source License
/** * Get HTTP Entity populated with data in a format specified by the current sync scheme *//*from w ww . ja va2 s .c o m*/ private void setHttpEntity(SyncDataFormat format) throws Exception { RequestBody body; switch (format) { case JSON: body = RequestBody.create(JSON, DataFormatUtil.makeJSONString(getParams())); log("setHttpEntity format JSON"); Util.logActivities(context, "setHttpEntity format JSON"); break; case XML: //TODO: Make parent node URL specific as well body = RequestBody.create(XML, DataFormatUtil.makeXMLString(getParams(), "payload", HTTP.UTF_8)); log("setHttpEntity format XML"); Util.logActivities(context, context.getString(R.string.http_entity_format, "XML")); break; case YAML: body = RequestBody.create(YAML, DataFormatUtil.makeYAMLString(getParams())); log("setHttpEntity format YAML"); Util.logActivities(context, context.getString(R.string.http_entity_format, "YAML")); break; case URLEncoded: log("setHttpEntity format URLEncoded"); FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder(); List<NameValuePair> params = getParams(); for (NameValuePair pair : params) { formEncodingBuilder.add(pair.getName(), pair.getValue()); } Util.logActivities(context, context.getString(R.string.http_entity_format, "URLEncoded")); body = formEncodingBuilder.build(); break; default: Util.logActivities(context, context.getString(R.string.invalid_data_format)); throw new Exception("Invalid data format"); } setRequestBody(body); }
From source file:org.apache.hadoop.hdfs.web.oauth2.ConfRefreshTokenBasedAccessTokenProvider.java
License:Apache License
void refresh() throws IOException { try {//from w w w . j av a 2 s. c om OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS); client.setReadTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS); String bodyString = Utils.postBody(GRANT_TYPE, REFRESH_TOKEN, REFRESH_TOKEN, refreshToken, CLIENT_ID, clientId); RequestBody body = RequestBody.create(URLENCODED, bodyString); Request request = new Request.Builder().url(refreshURL).post(body).build(); Response responseBody = client.newCall(request).execute(); if (responseBody.code() != HttpStatus.SC_OK) { throw new IllegalArgumentException("Received invalid http response: " + responseBody.code() + ", text = " + responseBody.toString()); } Map<?, ?> response = READER.readValue(responseBody.body().string()); String newExpiresIn = response.get(EXPIRES_IN).toString(); accessTokenTimer.setExpiresIn(newExpiresIn); accessToken = response.get(ACCESS_TOKEN).toString(); } catch (Exception e) { throw new IOException("Exception while refreshing access token", e); } }