Pass string variable from java to php function

I start java and php I have to pass the string variable from Java client to php server using WebView

Is this what I'm doing right?

In the java side:

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

and on the php side: ReceiveLocation.php

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

Is it correct to pass the Coords parameter from the Java Client to the PHP Server function?

+4
source share
4 answers

To pass a PHP parameter using the page URL, simply add ?key=value to the URL, where key is the parameter key (i.e. name) and value is its value (i.e. the data you are trying to pass) and then in a PHP script you can get the following parameter:

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

replacing 'key' with the actual parameter name.

So PHP reads HTTP GET variables. See This for more information: http://www.php.net/manual/en/reserved.variables.get.php

Please be careful when accepting variables from the outside: they must be "sanitized", especially if they will be used in database queries or printed on an HTML page.

+2
source

Change your code as follows

Java Code:

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

PHP code:

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

use $_GET['parameter_name'] in PHP if you send a parameter using the GET method

OR

use $_POST['parameter_name'] in PHP if you send a parameter using the POST method

and its alternative is also $_REQUEST if you send the parameter using POST or GET.

+1
source

On the Java side, you need to make a request. Since this is http, you should use a library like httpclient . Your request will contain the following data:

 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); 

Work on tutorials on the httpclient side.

On the PHP side, you need to read the published data ...

right now i found this that will help you: http://www.coderblog.de/sending-data-from-java-to-php-via-a-post-request/

0
source

Source: https://habr.com/ru/post/1434807/


All Articles