Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>At first, You put a class like following:</p> <pre><code>public class AsyncHttpPost extends AsyncTask&lt;String, String, String&gt; { interface Listener { void onResult(String result); } private Listener mListener; private HashMap&lt;String, String&gt; mData = null;// post data /** * constructor */ public AsyncHttpPost(HashMap&lt;String, String&gt; data) { mData = data; } public void setListener(Listener listener) { mListener = listener; } /** * background */ @Override protected String doInBackground(String... params) { byte[] result = null; String str = ""; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(params[0]);// in this case, params[0] is URL try { // set up post data ArrayList&lt;NameValuePair&gt; nameValuePair = new ArrayList&lt;NameValuePair&gt;(); Iterator&lt;String&gt; it = mData.keySet().iterator(); while (it.hasNext()) { String key = it.next(); nameValuePair.add(new BasicNameValuePair(key, mData.get(key))); } post.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8")); HttpResponse response = client.execute(post); StatusLine statusLine = response.getStatusLine(); if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){ result = EntityUtils.toByteArray(response.getEntity()); str = new String(result, "UTF-8"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (Exception e) { } return str; } /** * on getting result */ @Override protected void onPostExecute(String result) { // something... if (mListener != null) { mListener.onResult(result) } } } </code></pre> <p>Now. You just write some lines like following:</p> <pre><code>HashMap&lt;String, String&gt; data = new HashMap&lt;String, String&gt;(); data.put("key1", "value1"); data.put("key2", "value2"); AsyncHttpPost asyncHttpPost = new AsyncHttpPost(data); asyncHttpPost.setListener(new AsyncHttpPost.Listener(){ @Override public void onResult(String result) { // do something, using return value from network } }); asyncHttpPost.execute("http://example.com"); </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. 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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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