title: How to repeatedly get data from a servlet using an android app
body: <p>I have an android app which repeatedly reads some data from a java servlet and plots it on Google Maps. So far I have managed to obtain the data once but I have trouble while repeating the process.</p>
  
  <p>What I have tried is basically this(since I think the actual code is too long I'm only using necessary fragments of it. And the actual code runs without any errors.):</p>
  
  <pre><code>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&lt;String, String, Integer&gt; {
  
          @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&lt;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;
          }
  
      }
  
  }
  </code></pre>
  
  <p>As I cannot loop the entire AsyncTask I used a loop inside the AsyncTask to repeatedly fetch location from the server. But what happens here is that, it ends up as an infinite loop and doesn't show any run time errors.</p>
  
  <p>I am relatively new to android so do pardon any newbie errors I might have made.
  Thanks.</p>