【问题标题】:Sending HTTP post data from an Android application to a server with PHP inside of html在 html 中使用 PHP 将 HTTP 发布数据从 Android 应用程序发送到服务器
【发布时间】:2014-08-10 11:59:39
【问题描述】:

所以我最近开发了一个涉及 GPS 位置的 android 应用程序。然后,我将应用程序设置为使用服务器端的 PHP 定期将其位置发送到服务器。最初,我只是将 PHP 代码放在自己的文件中,而 PHP 代码正在写入 index.html,然后由 apache 服务器显示。今天我决定尝试使用 Google Maps JavaScript API,并尝试使用我的应用程序中的位置数据在 Google 地图上设置标记。虽然地图工作正常,但 php 代码似乎没有接收 HTTP 发布数据,或者它没有对它做任何事情。
这是我的 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) {

                                }
                            }

提前感谢您提供的任何帮助!

【问题讨论】:

  • 如果你提醒你的帖子值,它们有值吗?警报(纬度);
  • @Matt 不,当我提醒他们时,它说的是 =$latitude?>。所以我想我可能错误地将我的变量从 php 代码传输到 java 代码?
  • 顺便说一句,尽量不要使用这个:'=',如果我没记错的话它很快就会被弃用,或者我的同事说(在网上找不到它,如果我是,请纠正我错了!)。
  • 显然我的同事是对的 ;)
  • @Matt 好吧,我尝试将每个变量更改为 var latitude = ;,但现在地图根本无法加载

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


【解决方案1】:

使用&lt;?php echo 代替&lt;?=

不要使用 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; ?>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多