4

Data that is sent from android is not being received by PHP. From the looks of things, the android app seems to be connecting to the server just fine. But the $_POST array seems to be empty:

Android:

        URL url;
        HttpURLConnection urlConnection;

        try
        {
            url = new URL("http://10.0.2.2:80/Web%20/Practice/Android%20Login/phpserver.php");

        }catch(IOException ex)
        {
            ex.printStackTrace();
            return "Exception5";

        }

        try
        {

                urlConnection = (HttpURLConnection) url.openConnection();

           // urlConnection.setReadTimeout(15000);
           // urlConnection.setConnectTimeout(10000);
            Log.d(TAG, "URL Connection enabled");

            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);

            Log.d(TAG, "Params [0]: " + params[0] +  "   Params[1]: " + params[1]);
            // Append parameters to URL
            Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("Username", params[0])
                    .appendQueryParameter("PhoneNumber", params[1]);


            String query = builder.build().getEncodedQuery();
            Log.d(TAG, "Query: " + query);
            Log.d(TAG, "Append Parameters to URL");

            // Open Connection for sending data
            OutputStream os = null;

            try {
                os = urlConnection.getOutputStream();
            }catch(Exception ex)
            {
                ex.printStackTrace();
                Log.e("ConcentratorExport", ex.getMessage() );
            }

            Log.d(TAG, "OutPutStream connection");
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

            Log.d(TAG, "BufferedWriter established");
            writer.write(query);

            Log.d(TAG, "Writing to Server");

            writer.flush();
            writer.close();
            os.close();

            urlConnection.connect();
            Log.d(TAG, "Connected");

        }catch(IOException ex)
        {
            ex.printStackTrace();
            return "Exception6";
        }


        try
        {

            int response_code = urlConnection.getResponseCode();

            if(response_code != 200)
            {
                Log.d(TAG, "Post failed");
                Log.d(TAG, "Post failed with " + response_code);
            }

            if(response_code == HttpURLConnection.HTTP_OK)
                return "Successful!";
            else
                return "Unsuccessful";

        }
        catch(IOException ex)
        {
            ex.printStackTrace();
            return "Exception7";
        }
        finally {
            urlConnection.disconnect();
        }

PHP:

      <?PHP

          $rawdata = file_get_contents("php://input");

          echo $rawdata;

          if(isset($_POST['Username']) && isset($_POST['PhoneNumber']))
           {
              $username  = $_POST['Username'];
              $phone_num = $_POST['PhoneNumber'];
              echo "Data sent";
              echo $username;
              echo $phone_num;
           } 
           else
              echo "No content was sent";


        ?>

From my research, I used

         file_get_contents("php://input")

to get any HTTP body requests. When I press the button in the app and refresh the page; nothing prints to the page. Signaling that data is lost while in transit to the server? I'm not sure whats happening, and what causes this. The android app is running on an emulator.

logcat:

    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: URL Connection enabled
    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Params [0]: Gina   Params[1]: 2225550000
    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Query: Username=Gina&PhoneNumber=2225550000
    07-17 20:52:54.743 2895-7142/com.example.czar.connectingtoserver D/MSG: Append Parameters to URL
    07-17 20:52:54.747 2895-7142/com.example.czar.connectingtoserver D/MSG: OutPutStream connection
    07-17 20:52:54.747 2895-7142/com.example.czar.connectingtoserver D/MSG: BufferedWriter established
    07-17 20:52:54.748 2895-7142/com.example.czar.connectingtoserver D/MSG: Writing to Server
    07-17 20:52:54.748 2895-7142/com.example.czar.connectingtoserver D/MSG: Connected
    07-17 20:52:54.860 2895-2895/com.example.czar.connectingtoserver D/MSG: Successful!
    07-17 20:52:54.860 2895-2895/com.example.czar.connectingtoserver D/MSG: Printed on page

This is how data is being packaged in the android app:

        // Append parameters to URL
        Uri.Builder builder = new Uri.Builder()
                .appendQueryParameter("Username", params[0])
                .appendQueryParameter("PhoneNumber", params[1]);

Username and PhoneNumber are the keys used, and I'm using the same keys in the php code as indexes. Not exactly sure what the problem is. Could someone shed some light as to what can cause this?

  • 2
    You're appending to the URL - in turn creating them as `$_GET` parameters. I can almost guarantee that `var_dump($_POST)`/`var_dump($rawdata)` is empty. – Darren Jul 18 '16 at 01:09
  • I used var_dump on both $_POST and $rawdata. They are empty. But when I replace $_POST with $_GET, the page is still empty after the button is pressed and the tab has been refreshed. –  Jul 18 '16 at 02:34
  • whats buffling is the fact that the user here used the same technique as I did above, when it comes to android and PHP connection: http://androidcss.com/android/android-php-mysql-login-tutorial/ I have tried everything from changing all of my $_POST to $_GET in php. I even tried to change the .setRequestMethod to GET in the arguments. No thing is appearing on the webpage but a blank page. –  Jul 18 '16 at 21:03
  • When I place $_GET array outside of the if statement, I get the following error on the page Notice: Undefined index: Username in C:\Web\Practice\Android Login\phpserver.php on line 3. Signaling that maybe the data may not be reaching the server even when the button is pressed and the page is refreshed; since Username is one of the keys that is sent from android. –  Jul 19 '16 at 04:27
  • Have a look at this answer: https://stackoverflow.com/a/4206094/1663283 . A Post request must provide the params in the Headers, not in the URL's query string. – Gabriel Aug 26 '18 at 13:12

0 Answers0