【问题标题】:How to repeatedly get data from a servlet using an android app如何使用 android 应用程序从 servlet 重复获取数据
【发布时间】:2014-09-08 08:29:11
【问题描述】:

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

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

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 比较陌生,所以请原谅我可能犯的任何新手错误。 谢谢。

【问题讨论】:

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


    【解决方案1】:

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

    签出:http://developer.android.com/reference/java/util/TimerTask.html

    【讨论】:

      猜你喜欢
      • 2014-11-17
      • 2014-09-21
      • 2016-06-14
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      相关资源
      最近更新 更多