如何使用Android应用程序从servlet重复获取数据

时间:2014-07-17 12:46:45

标签: java android google-maps servlets android-asynctask

我有一个Android应用程序,它反复从java servlet中读取一些数据并在Google Maps上绘制它。到目前为止,我已经设法获取了一次数据,但在重复该过程时遇到了麻烦。

我所尝试的基本上是这样的(因为我认为实际的代码太长了,我只使用它的必要片段。实际的代码运行没有任何错误。):

class TrackOnGoogleMap extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // initialize everything...
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
        UpdateDriverLocationTask update = new UpdateDriverLocationTask();
        update.execute(selectedDriver, location);
    }

    class UpdateTaxiLocationTask extends AsyncTask<String, String, Integer> {

        @Override
        protected Integer doInBackground(String... params) {

            String tUser = params[0];
            String location = params[1];

            // fetch location from server 10 times
            for (int i=0; i<10; i++) {
                location = fetchTaxiLocation(tUser);
                Log.d("SelectTaxi", "Location: " + location);
                publishProgress(location);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return 0;
        }

        @Override
        protected void onProgressUpdate(String... loc) {
            super.onProgressUpdate(loc[0]);

            // the servlet returns location in the form of a String split by a ':'
            String[] results = loc[0].split(":");
            assert results.length == 2;
            double lat = Double.parseDouble(results[0]);
            double lng = Double.parseDouble(results[1]);

            taxiLocation = new LatLng(lat, lng);

            marker.setPosition(taxiLocation);

            CameraUpdate update = CameraUpdateFactory.newLatLngZoom(
                taxiLocation, 16);
            map.animateCamera(update);
        }

        String fetchTaxiLocation(String taxiUser) {
            String location = null;
            try {
                HttpURLConnection con = (HttpURLConnection) (new URL(url).openConnection());
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setRequestMethod("POST");
                con.connect();

                con.getOutputStream().write(("t_user=" + taxiUser).getBytes());
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                 location = br.readLine();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return location;
        }

    }

}

由于我无法循环整个AsyncTask,因此我在AsyncTask中使用了一个循环来重复从服务器获取位置。但这里发生的是,它最终成为一个无限循环并且不会显示任何运行时错误。

我对android比较新,所以请原谅我可能犯的任何新手错误。 感谢。

1 个答案:

答案 0 :(得分:1)

看起来你只是在调用异步任务一次。我会创建一个timerTask,以设定的间隔调用你的update.execute()方法。在你的onResume中,检查该计时器任务是否已经运行,如果没有启动一个新的..也一定要取消计时器任务和异步任务onPause

结帐:http://developer.android.com/reference/java/util/TimerTask.html