2012-04-24 6 views
3

서버에서 특정 PHP 함수를 호출하고 일부 매개 변수를 보내려고합니다. 지금까지는 HttpClient를 사용하여 PHP 파일을 열 수 있고 Json으로 데이터 전송을 실행하고이를 내 앱에서 보여줄 수있었습니다. 이제 특정 기능을 호출하고 매개 변수를 전송할 수있게하려면 어떻게해야합니까 ?? 죄송합니다. Android에서 해당 기능을 호출해야하는 저택이 아닙니다. 여기에 몇 가지 코드안드로이드에서 PHP 함수를 호출하는 방법?

: 사전에

try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost("http://10.0.2.2/posloviPodaci/index.php"); 
      HttpResponse response = httpClient.execute(httpPost); 
      HttpEntity entity = response.getEntity(); 

      is = entity.getContent(); 

     } catch (Exception e) { 
      Log.e("log_tag", "Error in http connection" + e.toString()); 
     } 

     // Convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 

      String line = "0"; 
      while((line = reader.readLine()) != null){ 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 

     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     // Parsing data 
     JSONArray jArray; 
     try { 
      jArray = new JSONArray(result); 
      JSONObject json_data = null; 

      items = new String[jArray.length()]; 

      for(int i = 0; i < jArray.length(); i++) { 
       json_data = jArray.getJSONObject(i); 
       items[i] = json_data.getString("naziv"); 
      } 

     } catch (Exception e) { 
      // TODO: handle exception 
     } 

감사합니다, 늑대.

답변

1

CakePHP와 같은 MVC 프레임 워크로 작업하는 경우 원하는 JSON을 출력하는 함수 경로를 만들면됩니다.

그렇지 않으면, 당신은 이와 같은 당신의 index.php의 상단에 간단한 무언가를 활용할 수 있습니다 :

<?php 
    function foo($bar) { echo $bar; } 
    if(isset($_GET['action']) && (strlen($_GET['action']) > 0)) { 
     switch($_GET['action']) : 
     case 'whatever': 
      echo json_encode(array('some data')); 
      break; 
     case 'rah': 
      foo(htmlentities($_GET['bar'])); 
      break; 
     endswitch; 
     exit; # stop execution. 
    } 
?> 

이것은 당신이 행동의 매개 변수를 사용하여 URL을 호출 할 수있게된다.

http://10.0.2.2/posloviPodaci/index.php?action=rah&bar=test

더 민감한 데이터를 전달해야하는 경우

http://10.0.2.2/posloviPodaci/index.php?action=whatever

, 난 당신이 $ _POST와 막대기 및 암호화의 형태를 사용하는 것이 좋습니다.

+0

죄송합니다. – Wolf87

-1

PHP 쪽에서 처리 할 수 ​​있습니다. command라는 필드와 아마도 인수 목록을 사용하여 Json 객체를 만듭니다. PHP는 말에

당신은 JSON을 디코딩 한 후

은 수행

난에서 Andriod 응용 프로그램을 일하고 있어요, 나는 거기에서 호출 할 것을 넣지 않았다
if($obj.command == "foo"){ 
    foo($obj.arg[0],$obj.arg[1]); 

} 
관련 문제