0

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.

4
  • 1
    WARNING: If you're just learning PHP, please, do not learn the obsolete mysql_query interface. It's awful and is being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. A guide like PHP The Right Way can help explain best practices. Always be absolutely sure your user parameters are properly escaped or you will have severe SQL injection bugs like you do here. Commented Jun 22, 2015 at 20:26
  • @tadman I really appreciate your input and will make the changes you advised based on the pointers given after I have found an acceptable solution to the central problem. I am grateful for your time and expertise. Commented Jun 22, 2015 at 20:29
  • It's kind of odd you're calling your HTTP wrapper mySql in the Java side of things. Why not just call that your API? Are you sure the request is being received by your PHP code? Is there an error on that side? I'm suspecting the call is never made (Java problem), is made to the wrong endpoint, or the PHP script errors out before doing anything meaningful. Check the access and error logs for your PHP. Commented Jun 22, 2015 at 20:30
  • @tadman In regards to the HTTP wrapper's name, I have renamed this for purposes of clarity to phpGET. Is there anything obvious at first instance in the java code that may be preventing the requests? I will try looking into error logs but I believe the request is never received. PHP code works fine when URL is manipulated manually in the browser. Commented Jun 22, 2015 at 20:35

2 Answers 2

1

Okay,

So I'm not exactly too sure how good of a solution this is, but irrespective, it does exactly what I set out to achieve. I was able to successfully send a PHP GET request to the .php file using the following:

public class phpGET extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... params) {

            try {

                URL url = new URL("http://www.domain.com/example.php?auth=David+Cameron");
                URLConnection conn = url.openConnection();
                InputStream is = conn.getInputStream();
                is.close();

            } catch (Exception e) {

                Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();

            }

            return null;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Step one is always "Get it working", and step two is "Get it working better". Just don't forget step two. There's probably a better solution using Apache HTTPComponents but this should do for now.
0

replace

 URL url = new URL("http://domain.com/example.php?auth=David+Cameron");
 URLConnection connection = url.openConnection();
 connection.connect();

with

URL obj = new URL("http://domain.com/example.php?auth=David+Cameron");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");

1 Comment

Don't forget to add code formatting with the {} button or indent everything four spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.