2010-12-16 83 views
0
try { 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = "http://somepostaddress.com"; 
    HttpPost post = new HttpPost(postURL); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("user", "kris")); 
     params.add(new BasicNameValuePair("pass", "xyz")); 
     UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
     post.setEntity(ent); 
     HttpResponse responsePOST = client.execute(post); 
     HttpEntity resEntity = responsePOST.getEntity(); 
     if (resEntity != null) {  
      Log.i("RESPONSE",EntityUtils.toString(resEntity)); 
     } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

這是我在http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests發現的代碼。之後我只會在php中使用簡單的POST方法變量,對吧?使用PHP GET和POST在Android編程

示例:

$ user = $ _ POST ['user']; $ pass = $ _ POST ['pass'];

但是,當我執行代碼時,PHP只顯示0。我可以知道如何解決它嗎?感謝

P/S:SRY的英語不好

回答

0

HttpClient的不喜歡上了POST發送的URL查詢字符串。雖然我沒有找到this ...

PostMethod.addParameter()和 PostMethod.setRequestBody()是 相互排斥的,因爲它們都 指定POST實體,只有一個 可以同時使用。如果你想 通過URI查詢發送參數 字符串請使用 HttpMethod.setQueryString(NameValuePair [])。

0

對於GET請求 - 只需將它放在參數行中即可。

POST請求 - 做到這一點:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("params1", value1)); 
nameValuePairs.add(new BasicNameValuePair("params2", value2)); 
... 

HttpPost requestMethod = new HttpPost(THE_URL); 

try { 
    ((HttpPost)requestMethod).setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
} catch (UnsupportedEncodingException e) { 
    e.printStackTrace(); 
} 

... 
client.execute(requestMethod); 
+0

感謝您的幫助。但是,它適用於仿真器嗎?值從來沒有顯示在我的PHP文件.... – Sam 2010-12-17 02:18:41

+0

我看到你解決了它:) – shein 2010-12-17 08:17:27

0

試試這個方法:

HttpURLConnection connection; 
    OutputStreamWriter request = null; 

    URL url = null;   
    String parameters = "username=username&password=password";   
    String response = null; 

    try 
    { 
     url = new URL("your login URL"); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoOutput(true); 
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     connection.setRequestMethod("POST");  

     request = new OutputStreamWriter(connection.getOutputStream()); 
     request.write(parameters); 
     request.flush(); 
     request.close(); 

     if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) 
     { 
      String line = ""; 

      InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
      BufferedReader reader = new BufferedReader(isr); 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      response = sb.toString(); 
      isr.close(); 
      reader.close(); 
     }   
     else 
     { 
      // error while connecting to the server. please try afetr some time. 
     } 
    } 
    catch(IOException e) 
    { 
     //Error 
    }