2010-12-16 9 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를 사용하십시오.

예 :

$ 사용자 = $ _ POST [ '사용자']; $ pass = $ _ POST [ 'pass'];

그러나 코드를 실행할 때 PHP는 0 만 표시합니다. 내가 어떻게 해결 해야할지 알아? 감사

P/s의 : 나쁜 영어

답변

0

HttpClient를위한 SRY는 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

+0

난 당신이 그것을 해결 볼 :) – shein

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 
    } 
관련 문제