2013-07-31 109 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