我试图通过这个 Android 应用程序将纬度和经度的输出传递到 MySQL 数据库,但我不知道从哪里开始。
package com.example.gpstracking;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidGPSTrackingActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
}
最佳答案
我不认为建立与 mysql 的直接连接是一个好主意(根本)。所以我建议执行以下操作:
构建一个简单的restful php应用程序,它将从您的Android应用程序以POST请求的形式接收您的数据,然后将其存储到MySQL数据库中。
- a framework to rapidly build a rest app in PHP
- intruduction and tutorial to google volley - how to make a POST request
我认为这个组合会做得很好而且干净。
替代方案:如果您认为这部分的 Restful 部分太过分了,您可以忽略这部分并使用简单的 php 脚本来完成。
关于java - 将变量从 Android 应用程序传递到 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506401/