Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've stayed away from using <code>URLConnection</code> and instead have been using <code>DefaultHttpClient</code>. Here are 2 simple methods which sends off either GET or POST and returns a <code>String</code> response</p> <p>The important part to note is where you add name -> value pairs to the <code>HttpPost</code> object: <code>nameValuePairs.add(new BasicNameValuePair(key, params.get(key)));</code></p> <p><code>httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));</code></p> <p>Here is an example:</p> <pre><code>Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(3); params.put("fname", "Jon"); params.put("ssn", "xxx-xx-xxxx"); params.put("lname", "Smith"); ... String response = execRequest("http://xxx.xxx.se/postReciverTest.php", params); </code></pre> <p>-</p> <pre><code>public static String execRequest(String url, Map&lt;String, String&gt; params) { try { DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); HttpPost httpPost = null; HttpGet httpGet = null; if(params == null || params.size() == 0) { httpGet = new HttpGet(url); httpGet.setHeader("Accept-Encoding", "gzip"); } else { httpPost = new HttpPost(url); httpPost.setHeader("Accept-Encoding", "gzip"); List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); for(String key: params.keySet()) { nameValuePairs.add(new BasicNameValuePair(key, params.get(key))); } httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } HttpResponse httpResponse = (HttpResponse)defaultHttpClient.execute(httpPost == null ? httpGet : httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if(null != httpEntity) { InputStream inputStream = httpEntity.getContent(); Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding"); if(contentEncoding != null &amp;&amp; contentEncoding.getValue().equalsIgnoreCase("gzip")) { inputStream = new GZIPInputStream(inputStream); } String responseString = Utils.convertStreamToString(inputStream); inputStream.close(); return responseString; } } catch(Throwable t) { if(Const.LOGGING) Log.e(TAG, t.toString(), t); } return null; } public static String convertStreamToString(InputStream inputStream) { byte[] bytes = new byte[1024]; StringBuilder sb = new StringBuilder(); int numRead = 0; try { while((numRead = inputStream.read(bytes)) != -1) sb.append(new String(bytes, 0, numRead)); } catch(IOException e) { if(Const.LOGGING) Log.e(TAG, e.toString(), e); } String response = sb.toString(); if(Const.LOGGING) Log.i(TAG, "response: " + response); return response; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload