我正在尝试创建一个应用程序,该应用程序通过 GPS 获取我的地理位置并将其发送到数据库。一切正常,直到我第二次尝试更新我的地理位置。我正在通过 ubuntu 终端将数据发送到模拟器中。当我第一次发送它时,它将映射指向该点,并将正确的数据插入数据库,但是当我第二次尝试发送数据时,它显示“不幸的是,应用程序已停止”)
我以这种方式发送数据:
telnet localhost 5554
geo fix 100 100
代码
public class LocationActivity extends MapActivity implements LocationListener {
...
InsertToDatabase Insert = new InsertToDatabase();
@Override
public void onLocationChanged(Location location) {
Insert.latitude = location.getLatitude();
Insert.longitude = location.getLongitude();
Insert.execute();
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
for (Address address : addresses) {
this.locationText.append("\n" + address.getAddressLine(0));
}
int latitude = (int)(location.getLatitude() * 1000000);
int longitude = (int)(location.getLongitude() * 1000000);
GeoPoint point = new GeoPoint(latitude,longitude);
mapController.animateTo(point);
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
InsertToDatabase 类(LocationActivity 类的子类)
class InsertToDatabase extends AsyncTask<String, String, String> {
Double latitude;
Double longitude;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LocationActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("longitude", this.longitude.toString()));
params.add(new BasicNameValuePair("latitude", this.latitude.toString()));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}