private String doPostRequest() throws IOException, ClientProtocolException { HttpPost request = new HttpPost(method.buildUrl()); request.addHeader("Content-Type", "application/json"); request.addHeader("Accept", "application/json"); Gson gson = GsonProvider.getGson(); String rawBody = gson.toJson(requestObject); Log.d("REMOTE_DO_POST", rawBody); request.setEntity(new StringEntity(rawBody)); StringBuilder sb = new StringBuilder(); Header[] headers = request.getAllHeaders(); for (Header header : headers) { sb.append(header.getName() + ":: " + header.getValue() + "::"); } return innerApiCall(request); }
// #{key}=#{URI.encode(val.to_s)}" // so could have add button in itemlist view which takes us to a form // should infer language settings from the list we are in // want to support addition of sound/image ... public CreateItemResult createItem( String cue, String cue_language, String character_cue_text, String part_of_speech, String response, String response_language, String character_response_text, String list_id) { String http_response = ""; int status_code = 0; AndroidHttpClient client = null; try { URI uri = new URI("http://api.smart.fm/items"); Log.d("DEBUG", uri.toString()); HttpPost post = new HttpPost(uri); // Main.consumer.setTokenWithSecret(Main.ACCESS_TOKEN, // Main.TOKEN_SECRET);// TODO store in preferences ... // Main.consumer.sign(post); // set POST body String post_body = "cue[text]=" + URLEncoder.encode(cue, "UTF-8") + "&cue[part_of_speech]=" + part_of_speech + "&cue[language]=" + cue_language + "&response[text]=" + URLEncoder.encode(response, "UTF-8") + "&response[language]=" + response_language + "&api_key=" + Main.API_KEY + "&list_id=" + list_id; if (character_response_text != null && !character_response_text.equals("")) { post_body += "&character_response[text]=" + URLEncoder.encode(character_response_text, "UTF-8"); } if (character_cue_text != null && !character_cue_text.equals("")) { post_body += "&cue[character]=" + URLEncoder.encode(character_cue_text, "UTF-8"); } Log.d("DEBUG", post_body); // username/password here has to match the API key? String auth = Main.username(this) + ":" + Main.password(this); byte[] bytes = auth.getBytes(); post.setHeader("Authorization", "Basic " + new String(Base64.encodeBase64(bytes))); // post.setHeader("Authorization", // "Basic dGFuc2FrdTpzYW1qb3NlcGg="); // [B@434f6610 // setting content-type is overwritten ... post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setHeader("Host", "api.smart.fm"); HttpEntity entity = new StringEntity(post_body, "UTF-8"); post.setEntity(entity); Header[] array = post.getAllHeaders(); for (Header h : array) { Log.d("DEBUG", h.toString()); } client = AndroidHttpClient.newInstance("Main"); HttpResponse response1 = client.execute(post); status_code = response1.getStatusLine().getStatusCode(); Log.d("DEBUG", response1.getStatusLine().toString()); array = response1.getAllHeaders(); for (Header h : array) { Log.d("DEBUG", h.toString()); } long length = response1.getEntity().getContentLength(); byte[] response_bytes = new byte[(int) length]; response1.getEntity().getContent().read(response_bytes); Log.d("DEBUG", new String(response_bytes)); http_response = new String(response_bytes); // HttpEntity entity = response1.getEntity(); } catch (IOException e) { /* Reset to Default image on any error. */ e.printStackTrace(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (client != null) { client.close(); } } return new CreateItemResult(status_code, http_response, list_id); }
public String post(@Nonnull String account, @Nonnull String resource, @Nonnull String body) throws CloudException, InternalException { if (logger.isTraceEnabled()) { logger.trace( "enter - " + AzureMethod.class.getName() + ".post(" + account + "," + resource + ")"); } if (wire.isDebugEnabled()) { wire.debug( "POST --------------------------------------------------------> " + endpoint + account + resource); wire.debug(""); } String requestId = null; try { HttpClient client = getClient(); String url = endpoint + account + resource; HttpPost post = new HttpPost(url); if (url.toLowerCase().contains("operations")) { post.addHeader("x-ms-version", "2014-02-01"); } else if (url.toLowerCase().contains("/deployments")) { post.addHeader("x-ms-version", "2014-05-01"); } else { post.addHeader("x-ms-version", "2012-03-01"); } if (strategy != null && strategy.getSendAsHeader()) { post.addHeader(strategy.getHeaderName(), strategy.getRequestId()); } // If it is networking configuration services if (url.endsWith("/services/networking/media")) { post.addHeader("Content-Type", "text/plain;charset=UTF-8"); } else { post.addHeader("Content-Type", "application/xml;charset=UTF-8"); } if (wire.isDebugEnabled()) { wire.debug(post.getRequestLine().toString()); for (Header header : post.getAllHeaders()) { wire.debug(header.getName() + ": " + header.getValue()); } wire.debug(""); if (body != null) { wire.debug(body); wire.debug(""); } } if (body != null) { try { if (url.endsWith("/services/networking/media")) { post.setEntity(new StringEntity(body, "text/plain", "utf-8")); } else { post.setEntity(new StringEntity(body, "application/xml", "utf-8")); } } catch (UnsupportedEncodingException e) { throw new InternalException(e); } } HttpResponse response; StatusLine status; try { response = client.execute(post); status = response.getStatusLine(); } catch (IOException e) { logger.error( "post(): Failed to execute HTTP request due to a cloud I/O error: " + e.getMessage()); if (logger.isTraceEnabled()) { e.printStackTrace(); } throw new CloudException(e); } if (logger.isDebugEnabled()) { logger.debug("post(): HTTP Status " + status); } Header[] headers = response.getAllHeaders(); if (wire.isDebugEnabled()) { wire.debug(status.toString()); } for (Header h : headers) { if (h.getValue() != null) { if (wire.isDebugEnabled()) { wire.debug(h.getName() + ": " + h.getValue().trim()); } if (h.getName().equalsIgnoreCase("x-ms-request-id")) { requestId = h.getValue().trim(); } } else { if (wire.isDebugEnabled()) { wire.debug(h.getName() + ":"); } } } if (wire.isDebugEnabled()) { wire.debug(""); } if (status.getStatusCode() != HttpServletResponse.SC_OK && status.getStatusCode() != HttpServletResponse.SC_CREATED && status.getStatusCode() != HttpServletResponse.SC_ACCEPTED) { logger.error("post(): Expected OK for GET request, got " + status.getStatusCode()); HttpEntity entity = response.getEntity(); if (entity == null) { throw new AzureException( CloudErrorType.GENERAL, status.getStatusCode(), status.getReasonPhrase(), "An error was returned without explanation"); } try { body = EntityUtils.toString(entity); } catch (IOException e) { throw new AzureException( CloudErrorType.GENERAL, status.getStatusCode(), status.getReasonPhrase(), e.getMessage()); } if (wire.isDebugEnabled()) { wire.debug(body); } wire.debug(""); AzureException.ExceptionItems items = AzureException.parseException(status.getStatusCode(), body); if (items == null) { throw new CloudException( CloudErrorType.GENERAL, status.getStatusCode(), "Unknown", "Unknown"); } logger.error( "post(): [" + status.getStatusCode() + " : " + items.message + "] " + items.details); throw new AzureException(items); } } finally { if (logger.isTraceEnabled()) { logger.trace("exit - " + AzureMethod.class.getName() + ".post()"); } if (wire.isDebugEnabled()) { wire.debug(""); wire.debug( "POST --------------------------------------------------------> " + endpoint + account + resource); } } return requestId; }