从Android的将数据发送到PHP服务器 [英] Sending data from Android to a PHP server

查看:347
本文介绍了从Android的将数据发送到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在android的应用程序,我想通过URL(送Android手机到Web PHP服务器的纬度和经度像= .. mydata.php纬度= 76867&放?;长= 87979 )。我有PHP的code,如果这个URL被击中数据库保存数据。

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.

所有我没有得到的是,如何通过我的Andr​​oid手机发送经纬度到PHP服务器。

All I am not getting is that how to send the latitude and longitude through my android mobile phone to the PHP server.

推荐答案

在@DarkXphenomenon答案大厦,请确保您有正确的权限。

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

然后在你的java类,你可以使用这个code将数据发送到PHP文件。确保捕获任何异常。

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();
        }

您还需要确保你的位置的变量,如经纬度是字符串,否则你会得到一些意外的错误。通常情况下,位置变量将是一个双重或浮动。一个双转换为字符串,excecute

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

这篇关于从Android的将数据发送到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆