我一直在尝试将经度和经度从我简单的Android应用程序发送到MySQL但却没有正面结果。 这是我的主要活动类我想知道如何将位置作为参数发送到sendcoordinates()方法或者是否有其他方法可用。
package com.pawee.getlocation;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
String coord;
@Override
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("Location", location.toString());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, locationListener);
}
}
public void sendcoordinates()
{
// name = ET_Name.getText().toString();
// user_pass = ET_Pass.getText().toString();
String method = "location";
geo backgroundTask = new geo(this);
backgroundTask.execute(method,coord);
finish();
}
}
这是将参数发送到MySql数据库的geo类。
package com.pawee.getlocation;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by Ahmadzai on 7/12/2017.
*/
public class geo extends AsyncTask<String, Void, String>{
Context ctx;
geo(Context ctx)
{
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
String coordd = "http://192.168.8.100:8081/geo/location.php";
String method = params[0];
if (method.equals("location"))
{
String coord = params[1];
try {
URL url = new URL(coordd);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("coord","UTF-8") + "=" +URLEncoder.encode(coord,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "stored ....";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return "Registration unsuccessful ....";
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
;
}
}
在数据库中,列的数据类型是几何。 如果你们中的任何人在这方面帮助我,或者建议我将精确或一点位置发送到数据库,我真的很感激。 谢谢