2014-09-26 116 views
-1

我的Android HTTPClient有一個問題。HTTPClient不發送HttpPost請求

我想發送POST數據到網站並在此之後解析內容。 我的問題是,POST數據沒有發送到網絡服務器。 我不知道爲什麼。

這裏是我的HTTP的Util類

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.List; 

import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.os.AsyncTask; 

public class HTTPHelperUtil { 

    private static HTTPHelperUtil instance; 

    private String url; 

    private List<NameValuePair> nameValuePairs; 

    private String result; 

    private HTTPHelperUtil() { 
     // nothing to do 
    } 

    public synchronized static HTTPHelperUtil getInstance() { 
     if (HTTPHelperUtil.instance == null) { 
      HTTPHelperUtil.instance = new HTTPHelperUtil(); 
     } 
     return HTTPHelperUtil.instance; 
    } 

    public HTTPHelperUtil init(String url, List<NameValuePair> nameValuePairs) { 
     this.url = url; 
     this.nameValuePairs = nameValuePairs; 
     return HTTPHelperUtil.instance; 
    } 

    public HTTPHelperUtil start() throws ClientProtocolException, IOException { 
     SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(); 
     AsyncTask<List<NameValuePair>, Void, String> execute = sendPostReqAsyncTask.execute(nameValuePairs); 
     return HTTPHelperUtil.instance; 
    } 

    class SendPostReqAsyncTask extends AsyncTask<List<NameValuePair>, Void, String> { 

     @Override 
     protected String doInBackground(List<NameValuePair>... params) { 
      String res = ""; 
      List<NameValuePair> postPairs = params[0]; 
      System.out.println(postPairs.get(0)); 
      System.out.println(postPairs.get(1)); 
      if (postPairs != null && !postPairs.isEmpty()) { 
       // Create a new HttpClient and Post Header 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(url); 
       try { 
        httppost.setEntity(new UrlEncodedFormEntity(postPairs)); 
        HttpResponse response; 
        response = httpclient.execute(httppost); 

        if (response != null && response.getEntity() != null) { 
         InputStream content = response.getEntity().getContent(); 
         if (content != null) { 
          BufferedReader reader = new BufferedReader(
            new InputStreamReader(content, "UTF-8")); 
          while (true) { 
           String addingSource = reader.readLine(); 
           if (addingSource != null) { 
            res = addingSource; 
            break; 
           } 
          } 
         } 
        } 
       } catch (IllegalStateException e) { 
        return "-"; 
       } catch (IOException e) { 
        return "-"; 
       } 
      } 
      System.out.println(res); 
      return res; 
     } 
     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      HTTPHelperUtil.this.result = result; 
     } 
    } 

    public String getResult() { 
     System.out.println("Result = " + result); 
     return result; 
    } 
} 

沒有從我的活動電話:

 final String url = "http://example.com/app/mysite.php"; 

     List<NameValuePair> postParams = new ArrayList<NameValuePair>(2); 
     postParams.add(new BasicNameValuePair("username", username)); 
     postParams.add(new BasicNameValuePair("password", password)); 
     HTTPHelperUtil.getInstance().init(url, postParams); 
     String result = ""; 
     try { 
      HTTPHelperUtil.getInstance().start(); 
      result = HTTPHelperUtil.getInstance().getResult(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

我的測試頁不通過POST獲取用戶名和密碼。

有沒有人在我的代碼中看到錯誤?

由於

+0

您是否使用過像Fiddler 2或CharlesProxy這樣的工具來確保請求確實會離開您的設備並轉到網絡服務器? – Pavlos 2014-09-26 14:25:56

+0

你確定有連接嗎? HTTPHelperUtil的catch塊中沒有e.printStackTrace()。你應該返回e.getMessage()(而不是「_」)。 'HTTPHelperUtil.getInstance()開始(); result = HTTPHelperUtil.getInstance()。getResult();'。這是行不通的。你想在asynctask開始後立刻得到結果。那是早。你必須等到asynctask完成。只有當onPostExecute執行結果時纔會出現。 ' – greenapps 2014-09-26 15:06:10

+0

是的,我有一個連接。 – JavaDM 2014-09-26 17:06:25

回答

0

按照您的代碼:

列表postParams =新的ArrayList(2);

在這裏,您在構造函數中傳遞2時,在聲明帶有名稱值對的列表時,這不是必需的。 這可能是因爲休息所有的代碼似乎很好。

+0

我不知道你的意思 – JavaDM 2014-09-27 11:02:45

+0

你正在傳遞整數2作爲參數os arraylist與namevalue collection..while在活動中創建列表 – harneev 2014-09-27 13:04:19

+0

這就是主動性。我在這裏沒有看到任何錯誤。 – JavaDM 2014-09-29 08:23:07