从android到mysql数据库插入数据时数据没有改变

时间:2012-05-08 12:10:04

标签: java php android mysql insert

我在向mysql插入数据(纬度和经度)时遇到问题。当我第一次运行应用程序时,插入了lat和long,但是当我更新位置(lat和long)并再次插入数据时,只有经度发生变化,并且纬度保持与插入的先前数据相同的值。我完全不知道为什么它不起作用。如果有人能帮助我,我会很高兴的。

这是我的代码

    package com.android.tyegah;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;


     import android.app.Activity;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class TyegahActivity extends Activity {
    private LocationManager locationManager;
private Location latestLocation;
private double lat;
private double longi;
private Button updateButton;
private TextView locUpdate;
public String url = "http://servername/file.php";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,onLocationChange);

    locUpdate = (TextView)findViewById(R.id.locUpdate);
    updateButton = (Button)findViewById(R.id.updateButton);
    updateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,onLocationChange);
            if (latestLocation.getLatitude() != lat && latestLocation.getLongitude() != longi ) {
                lat = latestLocation.getLatitude();
                longi = latestLocation.getLongitude();  
                }
            String latitude = Double.toString(lat);
            String longitude = Double.toString(longi);
            url  += "?latitude=" + latitude + "&longitude=" + longitude;
            locUpdate.setText(latitude + "," + longitude);
            getRequest(url);
            Toast.makeText(getBaseContext(),
                    "Location updated : Lat: " + latestLocation.getLatitude() +
                    " Lng: " + latestLocation.getLongitude(),
                    Toast.LENGTH_LONG).show();
            }
    }); 

 }


public void getRequest(String Url) {
    // TODO Auto-generated method stub
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = client.execute(request);

    } catch (Exception ex) {
        locUpdate.setText(ex.toString());
    }
}

LocationListener onLocationChange = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        latestLocation = location;
        if (latestLocation.getLatitude() != lat && latestLocation.getLongitude() != longi ) {
        Toast.makeText(getBaseContext(),
                "Location updated : Lat: " + latestLocation.getLatitude() +
                " Lng: " + latestLocation.getLongitude(),
                Toast.LENGTH_LONG).show();
        }
    }
};

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, onLocationChange);
}

}

这是php文件:

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("php"); 
$longitude = $_GET['longitude'];
$latitude = $_GET['latitude'];
    if($latitude && $longitude){
      $string= "insert into table (latitude, longitude)values('".$latitude."','".$longitude."')";
       mysql_query($string);
       }

mysql_close();
?>

2 个答案:

答案 0 :(得分:0)

只需更改两行并尝试

在android中 你写过

if (latestLocation.getLatitude() != lat && latestLocation.getLongitude() != longi )
而不是写

if (latestLocation.getLatitude() != lat || latestLocation.getLongitude() != longi )

和PHP

你已经推迟了

$string= "insert into table (latitude, longitude)values('".$latitude."','".$longitude."')";

代替

$string= "insert into table (latitude, longitude) values('".$latitude."','".$longitude."')";

尝试一下,我认为它会起作用

答案 1 :(得分:0)

您正在更新onCreate()方法的网址,请检查活动生命周期onCreate()方法将在创建活动或重新创建活动时第一次调用。 我建议你在为服务器请求创建url时建立一个回调机制来获取位置数据。