从Android应用程序连接和更新php服务器
我已经创建了一个gps应用程序,运行良好。现在我尝试的是将它连接到一个带有mysql数据库的php服务器上,其中所有人员的位置都会自动更新。。我是这部分的新用户。3.我试过一些,但我认为它写不出来。。有人能帮助我并指导我如何做的过程吗。。我们将非常感谢你的帮助。下面是我从frieds帮助和php脚本中编写的代码从Android应用程序连接和更新php服务器,php,android,mysql,latitude-longitude,Php,Android,Mysql,Latitude Longitude,我已经创建了一个gps应用程序,运行良好。现在我尝试的是将它连接到一个带有mysql数据库的php服务器上,其中所有人员的位置都会自动更新。。我是这部分的新用户。3.我试过一些,但我认为它写不出来。。有人能帮助我并指导我如何做的过程吗。。我们将非常感谢你的帮助。下面是我从frieds帮助和php脚本中编写的代码 public class Post extends LocService {{ TelephonyManager telephonyManager = (TelephonyManag
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());
}
}
您可以使用此代码通过post将参数发送到php,作为响应,您将获得inputstream。您面临哪些问题?你遇到错误了吗?实际上我刚刚写了代码。。写作部分正确吗。。?你能告诉我怎么做吗..receive=HttpPostExample.SendJsonUpdatejson,Sync_URL;上面一行和这两行出错,SQLiteDatabase db=databasehelper.getwriteabledatabase;Cursor Cursor=db.queryTABLE,null,null,null,null;我们很难检查整个代码并验证它。你为什么不运行它,看看它是否有效,如果无效,带着你面临的问题回来。谢谢你。。我就试试看。。还有一件事我需要用json写东西吗?不,你不需要用json写东西。从数据库中获取latitude和longitude的值后,只需将其与代码中的键一起发送。nameValuePairs列表是键-值对。此处左边的值是键,在php中使用相同的键检索值。很抱歉再次打扰您..我将代码放在何处。。。?在什么之间。。?
String url="http://location.net/storeg.php";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
nameValuePairs.add(new BasicNameValuePair("longitude", longitude));
nameValuePairs.add(new BasicNameValuePair("service", service));
nameValuePairs.add(new BasicNameValuePair("devid", devid));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream httpEntity = httpResponse.getEntity().getContent();