I am trying to get my Java program to send a GET request to the following (pseudo) PHP file:
www.domain.com/example.php
Here is the PHP script:
<?php
include "dbcredentials.php";
$query = "INSERT INTO accesscode (id) VALUES ('" . $_GET['auth'] . "')";
$result = mysql_query($query) or die(mysql_error()); // Check if query was successful
print ($_GET['auth']);
?>
The above php is fairly self explanatory. When a GET request is received, it prints the content of the request to the page and also adds it in the column "ID" in table named "accesscode".
Therefore if typing "www.domain.com/example.php?auth=Brad+Pitt", the PHP file will print Brad Pitt and add that to the table "accesscode".
Okay, simple enough. Except I want my Java program to be able to send a GET request too. So I devised the following function which is called by new phpGET().execute():
public class phpGET extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://domain.com/example.php?auth=David+Cameron");
URLConnection connection = url.openConnection();
connection.connect();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
}
Now just to confirm, the above does not catch an error. But for some reason, it must fail to send the GET request (http://domain.com/example.php?auth=David+Cameron) to the php file, on the basis that David Cameron is not added to the database after the function is called, as is intended.
I confirm I have the following Android manifest permissions:
<uses-permission android:name="android.permission.INTERNET" />
I'm pretty stumped on this one; any ideas?
Additional information: I am using Windows 7 x86, the latest Android Studio and testing on a Samsung Galaxy S5.
Also please note, I am aware, and it has been brought to my attention, that essentially I should be using PDO for database access, and that I should be taking additional action to prevent MySQL injection vulnerabilities that the code is riddled with. I would like to assure you that I intend on doing so upon solving the central problem discussed herein.