2012-04-07 61 views
0

我想使用HttpPost發送一個帶有一些參數的URL,但它沒有成功。我正在使用此代碼:HttpPost不能執行其功能

public void Exchange() 
{ 
    final ProgressDialog pd = ProgressDialog.show(this, null, ResourceBundle.getBundle("lang").getString("doTheAction"), true, false); 
     new Thread(){ 
       public void run() { 

        HttpClient httpclient = new DefaultHttpClient(); 
        try { 
         httpclient.execute(new HttpPost(URL)); 
         Merchant.this.runOnUiThread(new Runnable(){ 
          @Override 
           public void run(){ 
            Again(); 
           } 
          }); 

        } catch (IOException e) { e.printStackTrace(); } 
        finally { httpclient.getConnectionManager().shutdown(); } 
        pd.dismiss(); 
       } 
     }.start(); 
} 

它不會做任何事情,但它必須改變MySQL數據庫的某些值。有人能幫我嗎?看來這是完全正確的。

預先感謝您!

+0

一個HTTP-POST沒有POST值看起來有點沒用 – 2012-04-07 19:34:55

回答

1

您可以使用此功能將數據發佈到通過API服務器,那麼MySQL數據庫上更改的一些值是服務器端的任務

public static String post(String to, List <NameValuePair> params) { 
     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(to); 
      UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
      post.setEntity(ent); 
      HttpResponse responsePost = client.execute(post); 
      HttpEntity resEntityPost = responsePost.getEntity(); 
      if (resEntityPost != null) return EntityUtils.toString(resEntityPost); 
     } catch (Exception e) { 
      Log.e("POST", e.toString()); 
     } 
     return null; 
    } 
+0

非常感謝你許多! =) – 2012-04-07 19:49:11

0

您可能需要檢查httpClient.execute()返回的HttpResponse。您可能想要檢查response.getStatusLine().getStatusCode()也許它不是200.您可能還想檢查您的服務器日誌是否收到任何內容?你也確定IOException不被拋出?