// Returns params string only, e.g. // application_id=774&auth_key=aY7WwSRmu2-GbfA&nonce=1451135156 private String getEncodedParamsOnlyString(boolean encoded) { StringBuilder paramsOnly = new StringBuilder(); if (parameters.size() > 0) { for (String key : parameters.keySet()) { String value = parameters.get(key); if (value != null) { String encodedValue = encoded ? URLEncoder.encode(value) : value; paramsOnly.append(String.format("%s=%s&", key, encodedValue)); } } // remove last '&' paramsOnly.deleteCharAt(paramsOnly.length() - 1); } return paramsOnly.toString(); }
// create http request private HttpRequestBase getHttpRequest() { HttpRequestBase httpRequest = null; if (method == null) { method = RestMethod.GET; } // set method switch (method) { case GET: httpRequest = new HttpGet(getFinalURL().toString()); break; case DELETE: httpRequest = new HttpDelete(getFinalURL().toString()); break; case POST: HttpPost postRequest = new HttpPost(getFinalURL().toString()); if (multipartEntity != null) { postRequest.setEntity(multipartEntity); } else { postRequest.setEntity(getBody()); } httpRequest = postRequest; break; case PUT: HttpPut putRequest = new HttpPut(getFinalURL().toString()); putRequest.setEntity(getBody()); httpRequest = putRequest; break; default: break; } // set headers if (headers != null) { for (String key : headers.keySet()) { httpRequest.addHeader(key, headers.get(key)); } } return httpRequest; }
public HttpEntity getBody() { HttpEntity entity = null; if (body == null) { if ((method == RestMethod.POST || method == RestMethod.PUT) && parameters.size() > 0) { List<NameValuePair> bodyParams = new ArrayList<NameValuePair>(); for (String key : parameters.keySet()) { bodyParams.add(new BasicNameValuePair(key, parameters.get(key))); } try { entity = new UrlEncodedFormEntity(bodyParams, ConstsGlobal.DEFAULT_ENCODING); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } } } else { entity = body; } return entity; }