它已经很久了,经过大量研究后,我无法找到我真正想要的解决方案。我制作了一个 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
}
请一些人帮我解决这个...