【问题标题】:Pass string variable from java to php function将字符串变量从 java 传递给 php 函数
【发布时间】:2012-09-10 13:58:05
【问题描述】:

我是java和php初学者 我必须使用 WebView 将字符串变量从 Java 客户端传递到 php 服务器

我做的对吗?

在java端:

String Coords=CoordsString;
String PHPPagePath="http://10.0.2.2/ReceiveLocation.php?Coords=" + CoordsString"; 

在 php 方面: ReceiveLocation.php

 <?php
include('ConnectionFunctions.php');
Connection(); 
$x=$_GET['Coords'];
GetCoords($x);
function GetCoords($x)
{
   echo $x;
}   
?>

这是将参数 Coords 从 Java Client 传递到 PHP Server Function 的正确方法吗?

【问题讨论】:

  • 您的 PHP 代码中没有任何东西可以从外部获取任何变量...我不明白您希望 PHP 脚本做什么。
  • 我是一个初学者,我不知道该怎么做...你能帮帮我吗?
  • 查看我的回答:对于这样一个模糊的问题,我已尽我所能... :)
  • @gd1 感谢您的更正,这确实很有帮助,但我认为我的问题并不含糊,除了对当前代码的更正之外,其他用户还提供了替代解决方案。
  • 谢谢大家的帮助,我编辑了我的代码,它工作正常。我编辑了 java 和 php 代码=)

标签: java php android


【解决方案1】:

要使用页面的 URL 将参数传递给 PHP,只需将 ?key=value 附加到 URL,其中 key 是参数键(即名称),value 是它的值(即您的数据)正在尝试传输),然后在 PHP 脚本中您可以像这样检索参数:

<?php
    echo 'I have received this parameter: '.$_GET['key'];
?>

'key'替换为参数的实际名称。

这是 PHP 读取 HTTP GET 变量的方式。 更多信息请参见:http://www.php.net/manual/en/reserved.variables.get.php

在接受来自外部的变量时请小心:它们必须经过“清理”,特别是如果它们将用于数据库查询或打印在 HTML 页面中。

【讨论】:

    【解决方案2】:

    修改你的代码如下

    Java 代码:

    String Coords=CoordsString;
    String PHPPagePath="http://10.0.2.2/ReceiveLocation.php?Coords=10";
    

    PHP 代码:

    <?php
    include('ConnectionFunctions.php');
    Connection(); 
    
    function GetCoords(x)
    {
    echo 'Coords = '.$_GET['Coords'];
    
    }   
    ?>
    

    【讨论】:

      【解决方案3】:

      如果您使用GET 方法发送参数,请在PHP 中使用$_GET['parameter_name']

      如果您使用POST 方法发送参数,请在PHP 中使用$_POST['parameter_name']

      如果您使用 POST 或 GET 发送参数,它的备用选项也是 $_REQUEST

      【讨论】:

        【解决方案4】:

        在 Java 方面,您必须提出请求。由于这是 http,您应该使用像 httpclient 这样的库。您的请求将包含如下数据:

        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod("http://10.0.2.2/ReceiveLocation.php");
        method.addParamezter("Coord","1x3x4"); // or whatever
        int statusCode = client.executeMethod(method);
        

        在 httpclient 端学习教程。

        在 PHP 方面,您必须阅读发布的数据...

        现在我发现这对你有帮助:http://www.coderblog.de/sending-data-from-java-to-php-via-a-post-request/

        【讨论】:

          【解决方案5】:

          这个技巧是在php中处理java变量!!!'

          <?php
          $java_variable_in_php = 'string'; //Define our  variable.  
          ?>          
           <script> js_variable_name = "<?php echo $java_variable_in_php; ?>";
           string=' this is my trick to process java variable in php !!!';
           document.write(js_variable_name);//will print java variable name 'string'
           </script>
          
          
          
          <?php echo '<script>document.write(js_variable_name)</script>';?>
          <?php echo '<script>document.write('.$java_variable_in_php.')'.'</script>';?>
          

          检查输出的查看源!!希望这会对某人有所帮助...

          【讨论】:

            猜你喜欢
            • 2016-01-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-03
            • 2016-12-03
            • 1970-01-01
            相关资源
            最近更新 更多