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!