我创建了一个运行良好的 gps 应用程序。现在我正在尝试将它连接到带有 mysql 数据库的 php 服务器,其中所有人员的位置都会自动更新。我是这部分的新用户。我已经尝试了其中的一些,但我不认为它写..有人可以帮助我并指导我了解我应该如何做的过程..您的帮助将不胜感激。下面是我从 frieds 帮助和 php 脚本中编写的代码。
public class Post extends LocService {{
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = telephonyManager.getDeviceId();
//this is JSON part to put your information inside it
String postData = "{\"request\":{\"type\":\"locationinfo\"},\"userinfo\":{\"latitude\":\""+latitude+"\",\"longitude\":\""+longitude+"\",\"deviceid\":\""+deviceid+"\"}}";
HttpClient httpClient = new DefaultHttpClient();
// Post method to send data to server
HttpPost post = new HttpPost("http://location.net/storeg.php");
SQLiteDatabase db = databasehelper.getWritableDatabase();
Cursor cursor = db.query(TABLE, null, null, null, null);
cursor.moveToFirst();
while(cursor.isAfterLast() == false) {
if(cursor.getString(cursor.getColumnIndex("Sync")).equals("yes") ) {
String mob = cursor.getString(cursor.getColumnIndex("MobileID"));
String latitude = cursor.getString(cursor.getColumnIndex("Latitude"));
String longitude = cursor.getString(cursor.getColumnIndex("Longitude"));
String service = cursor.getString(cursor.getColumnIndex("Service"));
JSONObject json = new JSONObject();
try {
json.put("MobileID", mob);
json.put("Latitude", latitude);
json.put("Longitude", longitude);
json.put("Service", service);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
receive = HttpPostExample.SendJsonUpdate(json, Sync_URL);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(context, receive,Toast.LENGTH_SHORT).show();
}
cursor.moveToNext();
}
cursor.close();
try {
post.setURI(new URI("http://location.net/storeg.php"));
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// set your post data inside post method
try {
post.setEntity(new StringEntity(postData));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// execute post request here
try {
HttpResponse response = httpClient.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PHP 脚本
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mel_db", $con);
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$service = $_POST['service'];
$devid = $_POST['devid'];
$sql = "INSERT INTO `mehul_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);
?>
请帮助我完成这个过程......或者建议我应该如何做......
*已编辑: *得到此代码后,我可以使用它来获取更改变量后的纬度和经度.. 吗?
String result;
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
finally }
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}