lolliloop
lolliloop

Reputation: 399

HttpPOST in android

Hi can anybody enlighten me with this one. I'm already stock in it. I am trying to POST a reportcode into a web API, I didn't receive any error but when I check the site to see if there is the reportcode I post, I haven't seen it. I don't know if doing the right thing in HttpPost. Here is my code:

public class DoPost extends AsyncTask<String, Void, Boolean>

{ Exception exception = null; private ProgressDialog progressDialog; Context mContext = null; BufferedReader in; private String _code;

public DoPost(Context context, String code) 
{
    // TODO Auto-generated constructor stub
    mContext = context;
    this._code = code;
}

protected void onPreExecute() 
{     
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setMessage("Uploading....");
    progressDialog.show();              
    progressDialog.setCancelable(false);
}

@Override
protected Boolean doInBackground(String... arg0) 
{
    try{
        HttpParams httpParameters = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
        HttpConnectionParams.setSoTimeout(httpParameters, 15000);           

        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost("http://server.serving.com:0727/api/reports");

        httpPost.setHeader("Content-type", "application/json");

        JSONObject report = new JSONObject();
        report.put("ReportCode", _code);

         StringEntity entity1 = new StringEntity(report.toString(), HTTP.UTF_8);
         entity1.setContentType("application/json");
         httpPost.setEntity(entity1);

         Log.e("ReportCode",_code); 

    }catch (Exception e){
    Log.e("ClientServerDemo", "Error:", e);
    exception = e;
}
    return true;


}

@Override
protected void onPostExecute(Boolean valid)
{
    progressDialog.dismiss();   
    //Update the UI
    if(exception != null){
        Toast.makeText(mContext, exception.getMessage(), Toast.LENGTH_LONG).show();

    }else{
        Toast.makeText(mContext, "Uploaded.", Toast.LENGTH_SHORT).show();
        mContext.startActivity(new Intent(mContext, S_2nd_Main.class));
    }
}

}

Upvotes: 0

Views: 161

Answers (1)

Robertas
Robertas

Reputation: 26

You forgot to execute your POST request:

HttpResponse response = httpclient.execute(httpPost);

Upvotes: 1

Related Questions