POST を使用して JSON でデータを送信する正しい方法を行っているかどうかはわかりません。JSONObject「ReportCode」で単一のデータを送信したい。私はこれまでにこれを手に入れましたか?コードの何が問題なのか、データを JSON で渡すにはどうすればよいのか教えてください。ありがとう。
public class DoPost extends AsyncTask<String, Void, Boolean>
{
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<NameValuePair> pairs = new ArrayList<NameValuePair>();
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));
}
}
}