doujiao1981 2013-01-03 22:33
浏览 41

需要使用PHP将Android应用程序生成的纬度和经度值存储到mySQL数据库中

I have made an tracking app in android which successfully generates latitude and longitude

but I need to store lat and long in my external mySQL database using PHP.

I am using value type "decimal(10,7)" for lat and long in DB.

Any ideas how to store lat and long generated from android into my external mySQL database using PHP?

My PHP CODE

<?php 
        $lat = $_GET['lat'];
        $lng = $_GET['lng'];
        $sender=$_POST['sender'];

 $hostname = 'localhost';
 $username = 'xxxx';
 $password = 'xxxxxxx';

   $conn =  mysql_connect($hostname,$username,$password) or die('Error Please Try Again.'); 

if ($conn) { 

    mysql_select_db("android track", $conn); 


   $query2 = "INSERT INTO coor VALUES(NULL,'$sender','$lat','$lng')"; 

    if (mysql_query($query2, $conn)) 
        echo "Data Insertion Successful"; 
    else 
        echo "Oops".  mysql_error() ; 

  } 

 mysql_close();   
 ?>
  • 写回答

2条回答 默认 最新

  • doujia4759 2013-01-03 22:44
    关注

    You can submit the mLatand mLongvariables to the server using the code below:

    String storeURL = "http://www.yourdomain.com/yourscript.php?lat=" + mLat + "&long=" + mLong;
    
    URL getURL;
    getURL = new URL(storeURL);
    
    URLConnection connection = getURL.openConnection();
    connection.connect();
    
    InputStream input = new BufferedInputStream(getURL.openStream());
    OutputStream output = new ByteArrayOutputStream();          
    byte data[] = new byte[1024];
    
    while ((count = input.read(data)) != -1) {
     output.write(data, 0, count);
    }
    
    output.flush();
    output.close();
    input.close();
    
    String downloadResult = output.toString();
    Log.i("LOG", downloadResult); //Your php-script can, for example, "echo("OK");" if the storing was successful
    

    You still need to take care of error-handling (no network connection, etc.), of course.

    评论

报告相同问题?