List of usage examples for org.apache.http.client.methods HttpPost HttpPost
public HttpPost()
From source file:de.itomig.itoplib.GetItopJSON.java
/** * request data from itop server in json format * /*from ww w .j a va2 s . c o m*/ * @param operation * @param itopClass * @param key * @param output_fields * @return */ public static String postJsonToItopServer(String operation, String itopClass, String key, String output_fields) { AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); String result = ""; try { HttpPost request = new HttpPost(); String url = ItopConfig.getItopUrl(); String req = url + "/webservices/rest.php?version=1.0"; if (debug) Log.i(TAG, "req.=" + req); request.setURI(new URI(req)); ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(ItopConfig.getItopUserNameValuePair()); postParameters.add(ItopConfig.getItopPwdNameValuePair()); JSONObject jsd = new JSONObject(); jsd.put("operation", operation); jsd.put("class", itopClass); jsd.put("key", key); jsd.put("output_fields", output_fields); postParameters.add(new BasicNameValuePair("json_data", jsd.toString())); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); request.setEntity(formEntity); // request.addHeader(HTTP.CONTENT_TYPE, "application/json"); HttpResponse response = client.execute(request); String status = response.getStatusLine().toString(); if (debug) Log.i(TAG, "status: " + status); if (status.contains("200") && status.contains("OK")) { // request worked fine, retrieved some data InputStream instream = response.getEntity().getContent(); result = convertStreamToString(instream); Log.d(TAG, "result is: " + result); } else // some error in http response { Log.e(TAG, "Get data - http-ERROR: " + status); result = "ERROR: http status " + status; } } catch (Exception e) { // Toast does not work in background task Log.e(TAG, "Get data - " + e.toString()); result = "ERROR: " + e.toString(); } finally { client.close(); // needs to be done for androidhttpclient if (debug) Log.i(TAG, "...finally.. get data finished"); } return result; }
From source file:com.tweetlanes.android.urlservice.ApiService.java
public static HttpResponse postRequest(String url, String debugName) { HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(); HttpResponse response = null;// ww w . java2 s.c o m try { request.setURI(new URI(url)); //Log.d("tweetlanes url fetch", url); response = client.execute(request); //Log.d(TAG, debugName + " complete"); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return response; }
From source file:com.liferay.ide.kaleo.core.KaleoConnection.java
public JSONObject addKaleoDraftDefinition(String name, String titleMap, String definitionContent, int version, int draftVersion, long userId, long groupId) throws KaleoAPIException { JSONObject newKaleoDraftDefinition = null; HttpPost post = new HttpPost(); try {/*from www. ja v a 2s . co m*/ Object objects = new Object[] { addKaleoDraftDefinitionAPI(), "name", name, "groupId", groupId, "titleMap", titleMap, "content", definitionContent, "version", version, "draftVersion", draftVersion, "userId", userId, "serviceContext.userId", userId }; Object response = httpJSONAPI(post, objects); if ((response != null) && (response instanceof JSONObject)) { JSONObject responseObject = (JSONObject) response; if ((responseObject != null) && responseObject.has("exception")) { throw new KaleoAPIException(addKaleoDraftDefinitionAPI(), responseObject.getString("exception")); } newKaleoDraftDefinition = responseObject; } } catch (Exception e) { throw new KaleoAPIException(addKaleoDraftDefinitionAPI(), e); } return newKaleoDraftDefinition; }
From source file:com.github.restdriver.serverdriver.http.RequestBodyTest.java
@Test public void bodyAppliesItselfToRequestWhenContentTypeIncludesCharset() throws Exception { HttpPost request = new HttpPost(); RequestBody body = new RequestBody("content", "text/plain;charset=UTF-8"); body.applyTo(new ServerDriverHttpUriRequest(request)); assertThat(IOUtils.toString(request.getEntity().getContent()), is("content")); assertThat(request.getEntity().getContentType().getValue(), is("text/plain; charset=UTF-8")); assertThat(request.getFirstHeader("Content-type").getValue(), is("text/plain;charset=UTF-8")); }