从Android应用程序发送位置数据到apache服务器

时间:2014-06-18 19:52:58

标签: java php android apache http

我创建了一个应用程序,它接收手机的当前位置,然后将其发送到Apache Web服务器。我在服务器上使用php文件接收数据,然后将其写入HTML文件。该域工作正常,它正确显示我在Web服务器上的html文件,但php文件不会将数据写入html文件,我不知道为什么。

以下是发送数据的代码

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

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

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

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

      //Sending data to server via http client
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
      httpclient.execute(httppost);
                                } 
      catch (ClientProtocolException e) {
          // TODO Auto-generated catch block
                                } 
      catch (IOException e) {
          // TODO Auto-generated catch block
                                }
                            }
                        }

这是我的PHP代码

<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$latitude = $_POST["Latitude"];
$longitude = $_POST["Longitude"];
// specify the file where we will save the contents of the variable message
$filename="index.html";
// write (append) the data to the file
file_put_contents($filename,date('m/d/Y H:i:s') . " &nbsp &nbsp &nbsp" . $latitude . " &nbsp " . $longitude . "<br />",FILE_APPEND);
?>

感谢任何帮助!

0 个答案:

没有答案