我正在开发一个Android应用程序,它应该使用php将gps位置发送到mysql数据库。 我有Set_Location类,它在单击按钮时获取用户的gps位置,而BackgroundLocation类应该将这些坐标发送到mysql数据库。传递这些值时遇到问题。我在BackgroundLocation类上使用上下文并将值作为参数传递。这是我的Set_Location类:
package com.example.bensonkorir.m_ulinzi;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Set_Location extends AppCompatActivity implements LocationListener {
private Button button;
private TextView textView,latitude,longitude;
private LocationListener locationListener;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set__location);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
latitude = (TextView) findViewById(R.id.lat);
longitude = (TextView) findViewById(R.id.lon);
locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double lat= location.getLatitude();
double lon= location.getLongitude();
String type="location";
latitude.setText(Double.toString(lat));
longitude.setText(Double.toString(lon));
String lat2 = String.valueOf(location.getLatitude());
String lon2= String.valueOf(location.getLongitude());
BackgroundLocation backgroundLocation = new BackgroundLocation(this);
backgroundLocation.execute(type, lat2,lon2);
textView.append("\n"+location.getLatitude()+","+location.getLongitude());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
ConfigureButton();
}
private void ConfigureButton() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates("gps",5000,0,locationListener);
}
});
}
}
这是我的BackgroundLocation类:
package com.example.bensonkorir.m_ulinzi;
import android.app.AlertDialog;
import android.content.Context;
import android.location.LocationListener;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundLocation extends AsyncTask<String, Void, String> {
LocationListener locationListener;
AlertDialog alertDialog;
BackgroundLocation (LocationListener locationListener){ this.locationListener= locationListener;}
@Override
protected String doInBackground(String... params) {
String Register_Url= "http://192.168.43.254/register2.php";
String type = params[0];
if(type.equals("location")){
try {
String langitude=params[1];
String longitude=params[2];
URL url= new URL(Register_Url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data= URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(String.valueOf(langitude),"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(String.valueOf(longitude),"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line;
while ((line=bufferedReader.readLine())!=null){
result += line;
}
bufferedReader.close();;
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog= new AlertDialog.Builder((Context) locationListener).create();
alertDialog.setTitle(" Status");
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
}
请帮助我,或指出我正确的方向,谢谢。