0

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

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

{ 异常异常 = null; 私有 ProgressDialog 进度对话框;上下文 mContext = null; BufferedReader 中;私有字符串_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));
    }
}

}

4

1 回答 1

1

您忘记执行 POST 请求:

HttpResponse response = httpclient.execute(httpPost);
于 2013-07-31T08:13:44.303 回答