Sigterm
Sigterm

Reputation: 147

Posting info from Android to PHP Server

So I am writing an Android application, and part of it is supposed to use GPS to acquire longitude/latitude. And those values are supposed to post to a PHP server I also have running. The function I have posting is as follows:

public void doAll() {

Double locLat, locLon;
    try {
    locLat = locManager.getLastKnownLocation(locProvider).getLatitude();
    locLon = locManager.getLastKnownLocation(locProvider).getLongitude();
    } catch (NullPointerException e) {
    locLat = -1.0;
    locLon = -1.0;
    }
try {

    HttpClient client = new DefaultHttpClient();
    //we put all the parameters in the URL itself
    HttpPost request = new HttpPost("http://fatalatour.com/database.php?function=get_all&latitude="+Double.toString(locLat)+"&longitude="+Double.toString(locLon));

    ResponseHandler <String> responseHandler = new BasicResponseHandler();

   //List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
   //nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(locLat)));
   //nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(locLon)));
   //request.setEntity(new UrlEncodedFormEntity(nameValuePairs));

   String response = client.execute(request, responseHandler);
   Log.i("HOMI_RESPONSE", response.toString());

}

There is other code withing that but I believe that is all that actually affects the posting of locLat and locLon.

For the server end I have:

function get_all() {
            $lat = trim($_GET['latitude']);
            $lon = trim($_GET['longitude']);
            // DEBUG
            $f = fopen('debug.log', 'a');
            fprintf($f, 'get_all: '.print_r($_REQUEST, true)."\n");
            $result = mysql_query("SELECT * FROM homi_table");

            $num = 0; //# of homicides

            while ($row = mysql_fetch_assoc($result)) {
                    $d = distance($row['latitude'], $row['longitude'], $lat, $lon, "K");
                    if ($d < 100) { //number to change for distance filter
                            $num += 1;
        echo "fatality:".$row['slug']."~".strtoupper($row['name']).", Age ".$row['age']."\n".$row['date']."\nhttp://projects.latimes.com/homicide/post/".$row['slug']."~".$row['latitude']."~".$row['longitude'];


                            // GH: limiting the number of results to 100
                            if ($num == 100) {
                                    break;
                            }
                    }
            };
            echo $num;
            fprintf($f, "get_all: returning: ".$num."\n");
    }

Right now the issue seems to be the handling of $lat and $lon. When i hard code in a static number for each of those in the PHP the app works, yet when I leave it variable (as it should be), it fails. Any help would be greatly appreciated!

Upvotes: 3

Views: 356

Answers (2)

maggiomj
maggiomj

Reputation: 174

The URL you've constructed looks like an HTTP Get, not a Post. Post would have the data being posted in the body of the request.

If you don't mind the parameters being exposed in the URL, I would simply change HTTPPost to HTTPGet in the Android code. If you don't want that stuff to be in the URL, you'll have to rework the Android post a little bit (something similar to your commented out code I believe) and change the php code to access the $_POST variable instead.

Upvotes: 0

Piotr Kochański
Piotr Kochański

Reputation: 22672

In Android application you are using POST request, in PHP you read GET parameters. Nothing else come to my mind.

Upvotes: 1

Related Questions