简体   繁体   中英

Sending HTTP post data from an Android application to a server with PHP inside of html

So i recently developed an android application that involves GPS locations. I then set up the application to periodically send its location to a server using PHP on the server side. Originally, I just had the PHP code in its own file, and the PHP code was writing to index.html, which was then displayed by the apache server. Today I decided to experiment with the Google Maps JavaScript API, and try to use the location data from my application to set markers on a Google map. While the map works fine, it does not seem like the php code is receiving the HTTP post data, or if it is it isn't doing anything with it.
Here is my index.html file where I combined all of my html, php, and JavaScript code

<html>
<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$latitude = $_POST["Latitude"];
$longitude = $_POST["Longitude"];
$dateTime = date('m:d:y g:i:s');
?>
<title>Find My Location</title>
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map-canvas { height: 100% }
  #map-canvas { width: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=(My api key removed for privacy)">
</script>
<script type="text/javascript">
var latitude = '<?=$latitude?>';
var longitude = '<?=$longitude?>';
var dateTime = '<?=$dateTime?>';
var fmlLatLng = new google.maps.LatLng(latitude, longitude);
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(29.6520, -82.3250),
      zoom:10
    };
    var map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
    if (!isNaN(fmlLatLng.lat()) && !isNaN(fmlLatLng.lng())){
    var marker = new google.maps.Marker({
            position: fmlLatLng,
            map: map,
            title: 'Hello World!'

     });
       google.maps.event.addListener(marker, 'click', function() {
            alert(dateTime);

     });
    }

    }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body>
<head><strong>Recent Locations</strong></head>
<div id="map-canvas"/>
</body>
</html>

And this is the code in my android application being used to send the data

 if (currentLocation == null) {
                                return;
                            } else {

                                //Creating strings for the latitude and longitude values of our current location
                                String latitude = new String(" " + currentLocation.getLatitude());
                                String longitude = new String(" " + currentLocation.getLongitude());

                                // Creating an http client and http post object in order to interact with DFLGNVLAB01 server
                                HttpClient httpclient = new DefaultHttpClient();
                                HttpPost httppost = new HttpPost("http://findmylocation.chandlermatz.com/index.html");
                                try {

                                    //Creating identifier and value pairs for the information we want to send to DFLGNVLAB01
                                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                                    nameValuePairs.add(new BasicNameValuePair("Latitude", latitude));
                                    nameValuePairs.add(new BasicNameValuePair("Longitude", longitude));

                                    //Sending data to DFLGNVLAB01 via http client
                                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                                    httpclient.execute(httppost);
                                } catch (ClientProtocolException e) {

                                } catch (IOException e) {

                                }
                            }

Thanks in advance for any help you can give!

use <?php echo instead of <?=

Dont use json_encode...

try this:

<html>
<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$latitude = $_POST["Latitude"];
$longitude = $_POST["Longitude"];
$dateTime = date('m:d:y g:i:s');
?>
<title>Find My Location</title>
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map-canvas { height: 100% }
  #map-canvas { width: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=(My api key removed for privacy)">
</script>
<script type="text/javascript">
var latitude = '<?php echo $latitude; ?>';
var longitude = '<?php echo $longitude; ?>';
var dateTime = '<?php echo $dateTime; ?>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM