1

I am sending some values through HTTP Post to my server from my Android emulator but the values are not being stored. My logcat is showing response code 200 and displaying the codes of the php script as a http response. My database is fine as i am able to insert data in it. Any idea what might be the matter?

main.java

public void sendRegistrationIdToServer(String deviceId,
        String registrationId) {
    System.out.println(registrationId);
    System.out.println(deviceId);

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://10.21.78.11/storePost.php?");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        // Get the deviceID
        nameValuePairs.add(new BasicNameValuePair("devid", deviceId));
        nameValuePairs.add(new BasicNameValuePair("regid",
                registrationId));
        //HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));

        // Execute HTTP Post Request
        HttpResponse response = client.execute(post);
        int status = response.getStatusLine().getStatusCode();
        System.out.println("HTTP Status = "+status);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
            Log.e("HttpResponse", line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

storePost.php

<?php
mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db("DeviceID");
$regid = mysql_real_escape_string($_POST["regid"]);
$devid = mysql_real_escape_string($_POST["devid"]);
mysql_query("INSERT INTO Android(regID, devID) VALUES ('$regid', '$devid')") or die(mysql_error());
mysql_close();
?>
10
  • are you running on Emulator or device ? Commented Jan 10, 2012 at 7:50
  • What do you see when you open: 10.21.78.11/storePost.php in normal desktop browser? Commented Jan 10, 2012 at 7:54
  • I think you fogot this setDoInput( true ); in your code Commented Jan 10, 2012 at 7:55
  • @MichałPowaga I would see the codes of the php script. The address is actually false fyi (using another address). Commented Jan 10, 2012 at 7:57
  • @Arjun Which part of my code should I add that line to? Commented Jan 10, 2012 at 7:57

1 Answer 1

1

Make sure your web server is configured to run php codes. If you go to storePost.php with your desktop browser and you still see the codes displayed in the browser, then it's a configuration issue with your webserver. Until you fix that, the Android code will still be returning php codes as a response.

If you're on a windows platform, you might want to check this out http://www.simplehelp.net/2008/08/25/how-to-install-and-setup-apache-mysql-and-php-in-windows/

or linux https://help.ubuntu.com/community/ApacheMySQLPHP

Even easier, check xampp out www.apachefriends.org/en/xampp.html

Sign up to request clarification or add additional context in comments.

3 Comments

I was wondering if I would need to install php in order to run .php files? Btw, I'm using red hat. Anyway, I tried installing php but it says no package available.
Yes you need to install PHP. Try xampp apachefriends.org/en/xampp.html
Alright, I've installed xampp already. But now it's showing error mysql_connect() [function.mysql-connect]: [2002] No such file or directory (trying to connect via unix:///opt/lampp/var/mysql/mysql.sock) in /opt/lampp/htdocs/storePost.php on line 3

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.