[英]How to properly upload gps data from android app to XAMPP MYSQL using PHP POST Method?
I have read a number of questions on here dealing with this issue, but can't seem to quite make sense of my problem I have made a simple android app which retrieves location data when a button is pressed on screen. 我在这里已阅读了许多有关此问题的问题,但似乎无法完全理解我的问题,我制作了一个简单的android应用程序,当在屏幕上按下按钮时可以检索位置数据。 This pops up in a Toast widget as it ought to, but it is not committed to the MySQL db as I thought it would...
这会在Toast小部件中弹出,但它并没有像我想的那样提交给MySQL数据库。
I am using the apache.http.client and am sending the data (over a wifi network) using POST. 我正在使用apache.http.client并使用POST发送数据(通过wifi网络)。
Code follows: 代码如下:
package com.example.GpsTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class GpsTestActivity extends Activity {
//Set parameters for location updates
private static final long minimumDistanceChangeBeforeUpdate = 10; //Meters
private static final long minimumUpdatePeriod = 10000; //In Milliseconds
protected LocationManager locationManager;
protected Button gpsRequestButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gpsRequestButton = (Button) findViewById(R.id.gpsRequestButton);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minimumUpdatePeriod, minimumDistanceChangeBeforeUpdate, new MyLocationListener());
gpsRequestButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showCurrentLocation();
}
});
}
protected void showCurrentLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
String alert = String.format("Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
Toast.makeText(GpsTestActivity.this, alert, Toast.LENGTH_LONG).show();
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String alert = String.format("Location Updated \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
Toast.makeText(GpsTestActivity.this, alert, Toast.LENGTH_LONG).show();
postData(location);
}
public void onStatusChanged(String s, int i, Bundle b){
Toast.makeText(GpsTestActivity.this, "Provider Status Updated", Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(GpsTestActivity.this, "GPS Deactivated", Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(GpsTestActivity.this, "GPS Activated", Toast.LENGTH_LONG).show();
}
public void postData(Location location) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("192.168.1.229/appTesting/gpsdb.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("gpsLastKnown","location"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}}
And here is the simple PHP script I am using: 这是我正在使用的简单PHP脚本:
<?php
$con = mysql_connect("localhost","root","************");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("3dprotect", $con);
$sql="INSERT INTO gps (gpsLastKnown)
VALUES
('$_POST[gpsLastKnown]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
As it stands, no data is written to the db, no errors logged. 就目前而言,没有数据写入数据库,也没有记录错误。 Any help getting the app talking to the db would be much appreciated!
任何使应用程序与数据库对话的帮助将不胜感激!
Try adding braces to your $_POST array 尝试在$ _POST数组中添加括号
$sql="INSERT INTO gps (gpsLastKnown)
VALUES
('{$_POST[gpsLastKnown]}')";
Also, can you log what exactly is sent to the server. 另外,您是否可以记录发送到服务器的确切信息。 IE all the HTTP traffic.
IE的所有HTTP流量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.