有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我应该如何在mysql数据库中自动存储我的gps坐标?

我的代码中没有错误,我想学习的是在没有按钮的情况下将数据传递到数据库。目前,我必须先单击我的按钮,然后才能将gps坐标传递给数据库,但正如我所提到的,我希望在应用程序运行时传递它

这是我的密码:

**

public class Sample extends Activity{
    private ProgressDialog pDialog;
    JSONParser jsonParser = new JSONParser();

    EditText textLat;
    EditText textLong;

    // url to create new product
    private static String url_create_product = "http://10.0.2.2/SampleLocation/index.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        textLat = (EditText) findViewById(R.id.textLat);
        textLong = (EditText) findViewById(R.id.textLong);
        Button btnOK = (Button) findViewById(R.id.btnOK);
        //to run the location class
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new myLocationlistener();
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

        //to run the createnewproduct class
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new CreateNewProduct().execute();
            }
        });

    }
    class myLocationlistener  implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();
                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }

    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Sample.this);
            pDialog.setMessage("Sending Location");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String latitude = textLong.getText().toString();
            String longitude = textLat.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("latitude", latitude));
            params.add(new BasicNameValuePair("longitude", longitude));

            // 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) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), Echos.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } 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();
        }
    }   
}

**


共 (1) 个答案

  1. # 1 楼答案

    假设您已经实现了数据库(例如通过扩展SQLiteOpenHelper),那么您可以在侦听器的onLocationChanged()方法中调用对数据库的写入

    最好使用locationManager上对requestLocationUpdates的调用中的参数仔细控制写入数据库的频率。如果您有一个已经写入数据库的按钮,那么这应该很简单