javascript - 使用 html 中的 PHP 将 HTTP post 数据从 Android 应用程序发送到服务器

标签 javascript php android html google-maps-api-3

所以我最近开发了一个涉及 GPS 位置的 Android 应用程序。然后,我将应用程序设置为在服务器端使用 PHP 定期将其位置发送到服务器。最初,我只是将 PHP 代码放在自己的文件中,并且 PHP 代码写入 index.html,然后由 apache 服务器显示。今天,我决定尝试使用 Google Maps JavaScript API,并尝试使用应用程序中的位置数据在 Google map 上设置标记。虽然 map 工作正常,但 php 代码似乎没有接收 HTTP post 数据,或者即使是,也没有对其执行任何操作。
这是我的 index.html 文件,其中组合了所有 html、php 和 JavaScript 代码

<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>

这是我的 Android 应用程序中用于发送数据的代码

 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) {

                                }
                            }

预先感谢您提供的任何帮助!

最佳答案

使用<?php echo而不是<?=

不要使用 json_encode...

试试这个:

<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; ?>';

关于javascript - 使用 html 中的 PHP 将 HTTP post 数据从 Android 应用程序发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24315210/

相关文章:

Android:使用选项卡在不同 fragment 之间切换

javascript - jQuery 避免切换 div 在特定区域时隐藏

javascript - 单击一个按钮在 tone.js 中播放两个音符

php - 使用开始时间和结束时间之间的时间过滤数组或 XML

java - Gson在重建反序列化对象时丢失数据

android - 我应该如何在 Google Analytics v4 中跟踪 Fragments?

javascript - 从 list.js 的触发事件运行小部件不起作用

javascript - 如何在html5中检查音频文件是否存在

来自服务器时间戳的 JavaScript 异步时钟

php - Laravel 4 路由 - 如果不满足条件则能够跳过路由