Android中的HttpPOST

时间:2013-07-31 06:07:46

标签: android http-post

任何人都可以用这个启发我。我已经存货了。我正在尝试将报告代码发布到Web API中,我没有收到任何错误但是当我检查网站以查看是否有我发布的报告代码时,我还没有看到它。我不知道在HttpPost中做正确的事情。 这是我的代码:

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

{     异常异常= null;     private ProgressDialog progressDialog;     上下文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));
    }
}

}

1 个答案:

答案 0 :(得分:1)

您忘记执行POST请求:

HttpResponse response = httpclient.execute(httpPost);