0

I'm implementing an Android-MySQL interaction with PHP scripts. The script works fine when I was running it on my laptop.

When I was trying to handle the return value from the PHP scripts (returned by echo) in my Android application, it turns out the whole raw PHP script was returned.

I was using Genymotion as the emulator and I did set the url as 10.0.3.2 in my code.

While I was trying to visit the PHP page using Genymotion (10.0.3.2/test.php), the browser load the raw script as well.

Although I think its more like a port/configuration problem, below are my PHP scrips and Android code snippet.

PHP:

<?php

$db_name = "demo";
$mysql_username = "root";
$mysql_password = "password";
$server_name = "127.0.0.1";

$sql_query = "select * from table1;";

// establish connection to mysql database
$con = mysqli_connect($server_name, $mysql_username, $mysql_password, 
$db_name);

$result = mysqli_query($con, $sql_query);

$return_arr = array();

while ($row = mysqli_fetch_array($result))
{
    $return_arr[] = array(
        'id' => $row['id'],
        'size' => $row['size'],
        'material' => $row['material'],
        'date' => $row['date']
    );
}

header('Content-Type: application/json');
echo json_encode(array("server_response"=>$return_arr));

mysqli_close($con);

?>

Android:

System.out.println("nooo!");

        StringRequest stringRequest = new StringRequest(Request.Method.POST, json_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        });

        queueVolley.add(stringRequest);

The question is how make the PHP script return the correct data in Android? It was running on a MacBook Pro with Mojave OS.

Thank you!

  • 1
    Possible duplicate of [PHP code is not being executed, instead code shows on the page](https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – miken32 Nov 21 '18 at 04:34
  • @miken32, hi Mike, my PHP script actually works on my own laptop browser, it's just not working on the Android code and emulator. I checked the accepted answer in that thread and it was not quite my case. – Yezhen Wang Nov 21 '18 at 04:40
  • If your server is returning the PHP code, it's not configured properly, nothing to do with the client side. – miken32 Nov 21 '18 at 04:42
  • @miken32 but it is not returning PHP code and works fine when I'm visiting it on my laptop. – Yezhen Wang Nov 21 '18 at 04:44
  • What URL are you accessing? – miken32 Nov 21 '18 at 04:45
  • on my laptop: localhost:8080/test.php, in the Android code and Android browser: http://10.0.3.2/test.php – Yezhen Wang Nov 21 '18 at 04:47
  • Then the server running on port 8080 and the one running on port 80 must be configured differently. – miken32 Nov 21 '18 at 04:49
  • Can you please elaborate a little bit more? When I'm visiting 10.0.3.2:80 on the Android side, it was directed to 10.0.3.2 and shows "It works". – Yezhen Wang Nov 21 '18 at 04:52
  • I don't know what kind of setup you have going on, so I can't really say much more than that your web server is misconfigured. "It works" is a static page that comes with Apache. Your web server is working, but it's not configured to serve PHP. – miken32 Nov 21 '18 at 04:55
  • If you're using localhost:8080/test.php on the laptop, you should be using 10.0.3.2:8080/test.php in the emulator. Use the same port 8080 everywhere. – Joni Nov 21 '18 at 04:59
  • I followed an instruction online. Go to /Library/WebServer/Documents/the_folder_for_php_files, and run php -S localhost:8080 – Yezhen Wang Nov 21 '18 at 04:59
  • @Joni 10.0.3.2:8080 shows "ERR_CONNECTION_REFUSED" – Yezhen Wang Nov 21 '18 at 05:00
  • Restart the php server with `php -S 0:8080`. Either the server was no longer running, or the emulator is trying to connect to the wrong local IP, either way binding to all adressses (ip 0.0.0.0) should help – Joni Nov 21 '18 at 05:07
  • @Joni omg it worked! Could you please explain why is this working? Is it because it's now listening on all ports from 0-8080? – Yezhen Wang Nov 21 '18 at 05:09
  • Rather it's listening on port 8080 on all IPs configured on your machine. 0 is shorthand for 0.0.0.0 (https://en.m.wikipedia.org/wiki/0.0.0.0) – Joni Nov 21 '18 at 05:15

0 Answers0