0

I am having trouble sending a GCM registration_id to my database from Android. I have a script on my server that if I type "....name.php?regid=APA91bHmcD3_yZijf7dmmWlPiyi39Zx2kRTVHdI" it will work. However, when I try to send it through Android it won't.

Here is my code:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.domain.com/newnotifier.php");
    Log.d("TAG", regid);
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("regid", regid));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

Just need to know why this won't work. I look in my logcat and it shows the entire id being sent over.

Thanks,

EDIT: It doesn't give a error, it just doesn't show anything in the database. EDIT 2: PHP Code

$con = mysql_connect($mysql_host,$mysql_user,$mysql_password);
if (!$con)
{
   die('Could not connect: ' . mysql_error());
}

if ($con) 
{

}

 mysql_select_db($mysql_database, $con);
 name1 = $_REQUEST['regid'];
$sqldo = mysql_query("INSERT into users(regid) VALUES('".$name1."')");
Sohel Mansuri
  • 43
  • 1
  • 4
  • 1
    Post your PHP script. From you OG question, you have an example with GET where "it works", yet you are using Java code to POST. There is a difference between these two methods. – burmat Aug 06 '13 at 01:03

2 Answers2

0

In the example from your original question (that "works"), you are using GET to retrieve the value regid. When you go to send the value from your code above, it is using POST to send regid. Using the following should work in your script:

<?php
    $regid = $_POST['regid'];
?>

Also, add this to you try statement to get the server response:

HttpEntity httpEntity = httpResponse.getEntity();

This is a very bad way of doing it, but this is a cleaned up version of your deprecated code so that it should work. name1 in your original code needed to be $name1. You also had redundant and useless if statements. The echo statements will be returned to your device as httpEntity if you followed my last above tip:

$con = mysql_connect($mysql_host,$mysql_user,$mysql_password);

if ($con) {

    if (mysql_select_db($mysql_database, $con)) {
        $name1 = $_POST['regid'];
        $insert = "INSERT into users(regid) VALUES('$name1')";
        $sqldo = mysql_query($insert);

        if ($sqldo) {
            echo "query run";
        } else {
            echo "query failed";
        }

    } else {
        echo "could not select database";
    }

} else {
    echo "could not connect to server";
}

This WILL work, granted you use the correct credentials to connect to your database and you copy and paste this in correctly. And yes, you can use the variable directly in double quotes (") so that is why I did not use . to append the strings.

With all of that said, please read the following:

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
burmat
  • 2,548
  • 1
  • 23
  • 28
  • I just did. The thing is, when I do it manually through the browser, it works. When I do it through Android, it doesn't work.. is it possible that the string is too long to send through Android side? – Sohel Mansuri Aug 06 '13 at 01:24
  • @SohelMansuri No, your PHP code is messy and not correct. Please look into MySQLi or PDO because your code is being deprecated and will not function in the future. I did update my answer with what you were looking for, though. It should work just fine. – burmat Aug 06 '13 at 01:51
0

nameValuePairs are used to pass parameters for POST HTTP Request. However , i see that your php code is waiting a GET request (?regid).

Edit your url to

HttpPost httppost = new HttpPost("http://www.domain/newnotifier.php?param1=value1&param2=value...");
OWZY
  • 531
  • 7
  • 15