2013-07-31 69 views
0

嗨,有谁能够启发我这个。我已经存货了。我试图将一个报告代码发布到Web API中,但我没有收到任何错误,但是当我检查网站以查看是否有我发布的报告代码时,我没有看到它。我不知道是否在HttpPost中做正确的事情。 这是我的代码:HttpPOST在android中

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

{ 异常例外= NULL; 私人进展对话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)); 
    } 
} 

}

+0

亲切张贴堆栈跟踪这里 – KOTIOS

+0

我没有得到堆栈跟踪。 – lolliloop

+0

你是否试图记录你的回应? – KOTIOS

回答

1

你忘了执行您的POST请求:

HttpResponse response = httpclient.execute(httpPost); 
+0

我在添加时遇到错误.ClientProtocolException – lolliloop

+0

它可能是您请求或服务器响应错误。检查这个问题:http://stackoverflow.com/questions/12684739/clientprotocolexception-in-httpclient-executehttpget-responsehandler – Robertas