Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数据从android插入mysql数据库时数据未更改_Java_Php_Android_Mysql_Insert - Fatal编程技术网

Java 将数据从android插入mysql数据库时数据未更改

Java 将数据从android插入mysql数据库时数据未更改,java,php,android,mysql,insert,Java,Php,Android,Mysql,Insert,我在将数据纬度和经度插入mysql时遇到问题。当我第一次运行应用程序时,会插入lat和long,但当我更新位置lat和long并再次插入数据时,只有经度发生变化,纬度保持与先前插入的数据相同的值。我完全搞不懂它为什么不起作用。如果有人能帮我,我会很高兴的 这是我的密码 package com.android.tyegah; import org.apache.http.HttpResponse; import org.apache.http.client.HttpCli

我在将数据纬度和经度插入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();
?>

换两行试试看

在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."')";

试试看,我想它会起作用的

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

您尝试过getLastBestLocation方法吗?我没有。我不知道那种方法。但是谢谢你的建议。我试试看:你能给我举个简单的例子吗?谢谢你的回答。我将检查活动生命周期。