【发布时间】:2012-04-30 19:06:54
【问题描述】:
已经很久了,经过大量研究后,我无法找到我真正想要的解决方案。我制作了一个 GPS 跟踪应用程序。现在我想将我从手机获得的坐标发布到我的本地主机服务器。而且它也每 5 分钟发布一次坐标。我使用 WAMP 制作了一个服务器。
PHP 脚本
?php
echo 'Hello, world!';
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ric_db", $con);
$devid = $_POST["devid"];
$latitude = $_POST["latitude"];
$longitude = $_POST["longitude"];
$sql = "INSERT INTO `ric_db`.`locations` (
`id` ,
`devid` ,
`latitude` ,
`longitude` ,
`service`
)
VALUES (
NULL , '$devid', '$latitude', '$longitude', '$service'
);";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
?>
Java 文件
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
Context context;
public void onLocationChanged(Location loc)
{
StringBuilder sb = null;
String result = null;
InputStream is = null;
double latitude = loc.getLatitude();
double longitude = loc.getLongitude();
Toast.makeText(context, "Latitude:" +latitude + "Longitude:" +longitude, 5000).show();
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String Devid = telephonyManager.getDeviceId();
//this is JSON part to put your information inside it
String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"Devid\":\""+Devid+"\",\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\"}}";
String url="http://localhost/mehul/store.php";
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("Devid", Devid));
nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(latitude) ));
nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(longitude)));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity http_entity = response.getEntity();
Log.i("postData", response.getStatusLine().toString());
BufferedReader br = new BufferedReader(
new InputStreamReader(http_entity.getContent()));
String res = br.readLine();
System.out.println(res);
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
请帮我解决这个问题...
【问题讨论】:
-
请提供更多细节。我可以看出您正在尝试将数据放入服务器上的 mysql 数据库中,但您没有提到您遇到了什么错误或遇到了什么问题。
-
另外,您是使用数据交换格式 (json/xml) 还是只是将这些作为嵌入在请求中的 post/get 字段发送?
-
您在帖子中没有提出任何问题。请提出具体的编程问题。
-
实际上我是 json 新手,所以不完全知道该怎么做.. 有人可以帮助我吗.. gps 应用程序运行良好,只想发布它.. 我有什么变化在 mt php 中制作
-
您不需要在 MySQL 查询中再次指定数据库的名称。只需表名即可。而
$service似乎没有设置。此外,您的代码容易受到 SQL 注入的攻击,因此请使用mysql_real_escape_string转义您的用户输入。
标签: php android latitude-longitude