如何从Android调用PHP函数? [英] How to call PHP function from Android?

查看:126
本文介绍了如何从Android调用PHP函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话服务器的特定PHP函数,并且还发送一些参数。
到现在我实现了我可以使用的HttpClient打开PHP文件并执行数据传输到JSON,并表明我的应用程序。
所以,现在我希望能够调用特定的功能和参数发送给它,我怎么能做到这一点?
对不起,我没有豪宅,我需要调用该函数于Android。

I want to call specific php function on server and also to send some parameters. Till now I achieved that I can open php file using HttpClient and executed data transfer to Json and show that in my app. So, now I want to be able to call specific function and send parameter to it, how can I do that?? Sorry I didn't mansion that I need to call that function from Android.

下面一些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.

推荐答案

如果您正在使用MVC框架,如CakePHP的工作,你可以简单地创建一个函数的路线,将输出无论JSON你想。

If you are working with an MVC framework, such as CakePHP, you can simply create a route to a function that will output whatever JSON you'd like.

否则,
您可以利用的东西在你的index.php顶部简单的像这样的:

Otherwise, You can utilize something simple at the top of your index.php such as this:

<?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与动作的参数。

This will let you call the url with a parameter of action.

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

http://10.0.2.2/posloviPodaci/index.php?行动= RAH&安培;巴=测试

如果你需要传递更多敏感数据,我建议你坚持使用$ _ POST和使用某种形式的加密。

If you need to pass more sensitive data, I recommend you stick with $_POST and utilize some form of encryption.

这篇关于如何从Android调用PHP函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆