I am having an application in android and I want to send the latitude and longitude of the android mobile to the web PHP server through a url (like=..mydata.php?lat=76867&long=87979). I am having the php code that saves the data in database if this url is hit.
All I am not getting is that how to send the latitude and longitude through my android mobile phone to the PHP server.
Building upon @DarkXphenomenon answer, make sure you have the right permissions.
// to retrieve location
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
// to retrieve location
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
// to send data
<uses-permission android:name="android.permission.INTERNET"/>
Then in your java class you can use this code to send the data to the php file. Make sure to catch any exceptions.
String and = "&";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://my site.com/gpsdata.php?imei="+imei+and+"lat="+lat+and+"lng="+lng+and+"alt="+alt+and+"spd="+speed);
try {
httpclient.execute(httppost);
Log.d(TAG, "Data sent!");
} catch (ClientProtocolException e) {
Toast.makeText(this,
"Client protokol exception ", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this,
"IO exception " + e.getMessage(), Toast.LENGTH_LONG)
.show();
}
You also need to make sure that your location variables such as latitude or longitude are strings otherwise you will get some unexpected errors. Normally the location variables would be a double or a float. To convert a double to a string, excecute
String Longitude = Double.toString(doubleToConvertToString);
And likewise a Float to a String
String Longitude = Float.toString(doubleToConvertToString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With