6

我是初学者 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 的正确方法吗?

4

5 回答 5

2

要使用页面的 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 页面中。

于 2012-09-18T10:09:12.797 回答
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'];

}   
?>
于 2012-09-18T10:15:57.020 回答
1

如果您使用方法发送参数,请使用$_GET['parameter_name']inPHPGET

或者

如果您使用方法发送参数,请使用$_POST['parameter_name']inPHPPOST

$_REQUEST如果您使用 POST 或 GET 发送参数,它的替代方法也是如此。

于 2012-09-18T10:16:41.253 回答
0

在 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/

于 2012-09-18T10:17:57.703 回答
0

这个技巧是在 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>';?>

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

于 2019-02-07T23:59:05.590 回答