Adam Sturge
Adam Sturge

Reputation: 57

HTTP Post not working with PHP in android

I learning android and trying to write some code to verify a username and password using a PHP script and a WAMP server. I keep getting undefined index errors from my PHP script. As far as I can ascertain that means my PHP script can't access the data from the URL. Here is the relevant code. Any help is appreciated.

//build url data to be sent to server
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("username",username));
        nameValuePairs.add(new BasicNameValuePair("password",password));

        String result = "";
        InputStream is = null;
        //http post
        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/PasswordCheck.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }catch(Exception e){
            Log.e("Connection", "Error in http connection "+e.toString());
        }

Here is my PHP script

<?php
mysql_connect("localhost", "root", "") or die("could not connect to mysql");
mysql_select_db("drop-in") or die("database not found");

$username = $_POST["username"];
$suppliedPassword = $_POST["password"];
$databasePassword = "";
$output = "false";
$query = mysql_query("SELECT Password FROM users WHERE Username = '$username'") or die("query failed");
if(mysql_num_rows($query) > 0){
    $row = mysql_fetch_assoc($query); 
    $databasePassword = $row['password'];
    if($databasePassword == $suppliedPassword)
    {
        $output = "true";
    }
}
    print($output);
    mysql_close();
?>

And here a picture of the server's reply

https://i.sstatic.net/Fhfbv.jpg

EDIT: So I figured out that even though the PHP script is giving these errors the $username and $password variables contain the values my android app was attempting to pass along. However the presence of these errors is still messing with my code because the HTML for the error tables gets sent back to the android app in the response

Upvotes: 0

Views: 614

Answers (3)

sudhakar phad
sudhakar phad

Reputation: 191

You can try json_decode() function in PHP:

try to create A file on your server and put content of android request into simple text file you will get array output. in that file.

on your server in .php file write below code:

<?php
$data=file_put_content(collect.txt, $_POST);
 // if you got to know object or array name from txt file use it.
$array=json_decode($data, true) // for array output.
print_r($array);
?>

and if you don't want to read from file you can directly use if you know array name, in json object

<?php

$array=json_decode($data) // remove true for use as an object output.
print_r($array);
?>

once you got your array assign values to new variables and do whatever you want with them. Update database or anything as per your requiremnet

Upvotes: 0

Adam Sturge
Adam Sturge

Reputation: 57

Turns out it was a simple spelling error. I used a lower case 'p' in the word 'password' in my loop instead of an uppercase. Odd that it caused the error that it did.

Upvotes: 0

user2629998
user2629998

Reputation:

To me it looks like your Android code isn't POSTing the "username" and "password" fields, that explains why the PHP script can't find them.

In your code, new ArrayList<NameValuePair>();, the length of the arrayList may be missing, by looking at this code sample: it looks like it should be new ArrayList<NameValuePair>(2);, you should try with that and see if resolves the issue.

Upvotes: 1

Related Questions