How to call PHP function from Android?

I want to call a specific php function on the server, and also send some parameters. So far, I have ensured that I can open the php file using HttpClient and do the Json data transfer and show it in my application. So now I want to be able to call a specific function and send it a parameter, how can I do this? Sorry, I'm not a mansion, I need to call this function from Android.

here is some code:

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
        }

Thanks in advance, Wolf.

+3
source share
2 answers

If you work with an MVC framework like CakePHP, you can simply create a route to a function that will output whatever JSON you need.

, - 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=whatever

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

, $_POST .

+1

php. Json , , , , .

php- , json, :

if($obj.command == "foo"){
  foo($obj.arg[0],$obj.arg[1]);

}
-1

All Articles