Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use POST in sending data in JSON in android?
    text
    copied!<p>I don't know if I'm doing the right way of sending data in JSON using POST. I want to send a single data on JSONObject "ReportCode". I got this so far? Please tell me what's wrong on my code and what should I need to do to pass the data in JSON. Thanks.</p> <pre><code>public class DoPost extends AsyncTask&lt;String, Void, Boolean&gt; { Exception exception = null; private ProgressDialog progressDialog; Context mContext = null; BufferedReader in; InputStream is; public DoPost(Context context) { mContext = context; } protected void onPreExecute() { progressDialog = new ProgressDialog(mContext); progressDialog.setMessage("Validating...."); progressDialog.show(); progressDialog.setCancelable(false); } @Override protected Boolean doInBackground(String... arg0) { JSONObject jObject = new JSONObject(); try{ jObject.put("ReportCode","13-T001"); List&lt;NameValuePair&gt; pairs = new ArrayList&lt;NameValuePair&gt;(); pairs.add(new BasicNameValuePair("ReportData", jObject.toString())); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://phsjulchs4.tias.com.ph:1217/api/report"); httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8")); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); }catch (Exception e){ Log.e("ClientServerDemo", "Error:", e); exception = e; //Toast.makeText(getApplicationContext(), e.getMessage().toString(), 5).show(); } return true; } @Override protected void onPostExecute(Boolean valid) { progressDialog.dismiss(); //Update the UI if(exception != null){ Toast.makeText(mContext, "Sent", 6).show(); }else{ // mContext.startActivity(new Intent(mContext, S_2nd_Main.class)); } } } </code></pre>
 

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