List of usage examples for org.apache.http.client.entity UrlEncodedFormEntity toString
public String toString()
From source file:info.guardianproject.net.http.HttpManager.java
public static String doPost(String serviceEndpoint, Properties props) throws Exception { DefaultHttpClient httpClient = new SocksHttpClient(); HttpPost request = new HttpPost(serviceEndpoint); HttpResponse response = null;/*from ww w.j ava2 s.co m*/ HttpEntity entity = null; StringBuffer sbResponse = new StringBuffer(); Enumeration<Object> enumProps = props.keys(); String key, value = null; List<NameValuePair> nvps = new ArrayList<NameValuePair>(); while (enumProps.hasMoreElements()) { key = (String) enumProps.nextElement(); value = (String) props.get(key); nvps.add(new BasicNameValuePair(key, value)); Log.i(TAG, "adding nvp:" + key + "=" + value); } UrlEncodedFormEntity uf = new UrlEncodedFormEntity(nvps, HTTP.UTF_8); Log.i(TAG, uf.toString()); request.setEntity(uf); request.setHeader("Content-Type", POST_MIME_TYPE); Log.i(TAG, "http post request: " + request.toString()); // Post, check and show the result (not really spectacular, but works): response = httpClient.execute(request); entity = response.getEntity(); int status = response.getStatusLine().getStatusCode(); // we assume that the response body contains the error message if (status != HttpStatus.SC_OK) { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); entity.writeTo(ostream); Log.e(TAG, " error status code=" + status); Log.e(TAG, ostream.toString()); return null; } else { InputStream content = response.getEntity().getContent(); // <consume response> BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) sbResponse.append(line); content.close(); // this will also close the connection return sbResponse.toString(); } }
From source file:com.openideals.android.net.HttpManager.java
public static String doPost(String serviceEndpoint, Properties props) throws Exception { HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost(serviceEndpoint); HttpResponse response = null;/*ww w .j av a2s . co m*/ HttpEntity entity = null; StringBuffer sbResponse = new StringBuffer(); Enumeration<Object> enumProps = props.keys(); String key, value = null; List<NameValuePair> nvps = new ArrayList<NameValuePair>(); while (enumProps.hasMoreElements()) { key = (String) enumProps.nextElement(); value = (String) props.get(key); nvps.add(new BasicNameValuePair(key, value)); Log.i(TAG, "adding nvp:" + key + "=" + value); } UrlEncodedFormEntity uf = new UrlEncodedFormEntity(nvps, HTTP.UTF_8); Log.i(TAG, uf.toString()); request.setEntity(uf); request.setHeader("Content-Type", POST_MIME_TYPE); Log.i(TAG, "http post request: " + request.toString()); // Post, check and show the result (not really spectacular, but works): response = httpClient.execute(request); entity = response.getEntity(); int status = response.getStatusLine().getStatusCode(); // we assume that the response body contains the error message if (status != HttpStatus.SC_OK) { ByteArrayOutputStream ostream = new ByteArrayOutputStream(); entity.writeTo(ostream); Log.e(TAG, " error status code=" + status); Log.e(TAG, ostream.toString()); return null; } else { InputStream content = response.getEntity().getContent(); // <consume response> BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) sbResponse.append(line); content.close(); // this will also close the connection return sbResponse.toString(); } }
From source file:com.mozilla.simplepush.simplepushdemoapp.MainActivity.java
/** Send the notification to SimplePush * * This is normally what the App Server would do. We're handling it ourselves because * this app is self-contained./*from www . j ava 2s. c o m*/ * * @param data the notification string. */ private void SendNotification(final String data) { if (PushEndpoint.length() == 0) { return; } // Run as a background task, passing the data string and getting a success bool. new AsyncTask<String, Void, Boolean>() { @Override protected Boolean doInBackground(String... params) { HttpPut req = new HttpPut(PushEndpoint); // HttpParams is NOT what you use here. // NameValuePairs is just a simple hash. List<NameValuePair> rparams = new ArrayList<>(2); rparams.add(new BasicNameValuePair("version", String.valueOf(System.currentTimeMillis()))); // While we're just using a simple string here, there's no reason you couldn't // have this be up to 4K of JSON. Just make sure that the GcmIntentService handler // knows what to do with it. rparams.add(new BasicNameValuePair("data", data)); Log.i(TAG, "Sending data: " + data); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(rparams); Log.i(TAG, "params:" + rparams.toString() + " entity: " + entity.toString()); entity.setContentType("application/x-www-form-urlencoded"); entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "text/plain;charset=UTF-8")); req.setEntity(entity); req.addHeader("Authorization", genSignature(entity)); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse resp = client.execute(req); int code = resp.getStatusLine().getStatusCode(); if (code >= 200 && code < 300) { return true; } } catch (ClientProtocolException x) { Log.e(TAG, "Could not send Notification " + x); } catch (IOException x) { Log.e(TAG, "Could not send Notification (io) " + x); } catch (Exception e) { Log.e(TAG, "flaming crapsticks", e); } return false; } /** Report back on what just happened. * * @param success Status of post execution */ @Override protected void onPostExecute(Boolean success) { String msg = "was"; if (!success) { msg = "was not"; } mDisplay.setText("Message " + msg + " sent"); } }.execute(data, null, null); }