title: Android : Get data in real-time instead of using Async-Task
body: <p>I am relatively new to Android programming, but I do come from a Java background, building back-end stuff for Enterprise applications. Now, I am working on an Android project in which I would like to retrieve a java.util.List of Restaurants from the server. </p>
  
  <p>Currently, to achieve these tasks, I am using a simple Thread with a new Runnable, as the thread is immediately in Thread-pool and I can then simply join the Thread, and wait till it receives a reply from the server and then send it back. Very convenient this is and working good. </p>
  
  <p>However, there are two things I cannot do with Threads, check for Internet-connection on the client-side and display a progress-bar. As far as I understood and read, these both things can be achieved by AsyncTask. </p>
  
  <p>But here is the critical point, I don't want the AsyncTask to execute in some near future and then return the result, because with my current Thread model, I can simply join the Thread and relax.</p>
  
  <p>Now, my question is, is the same possible in AsyncTask, I don't mean using get method after 1000msec. I meant actually waiting for it. How do I accomplish that? And with that, I want to know how do I check for INternet, and if successful, then only make a request. </p>
  
  <p>Here is one of the Async methods I have in the project and then the next is the simple Thread model I have in the project.</p>
  
  <pre><code> private class LoginUserViaRest extends AsyncTask&lt;Void, Void, String&gt; {
  
          @Override
          protected String doInBackground(Void... params) {
  
              final EditText userEmail = (EditText) findViewById(R.id.usernameText);
              final EditText userPassword = (EditText) findViewById(R.id.PasswordField);
  
              rest.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
              StaticRestTemplate.jsessionid = rest.execute(StaticRestTemplate.baseURL+"j_spring_security_check", HttpMethod.POST,
                      new RequestCallback() {
                          @Override
                          public void doWithRequest(ClientHttpRequest request) throws IOException {
                              request.getBody().write(("j_username=" + userEmail.getText().toString() + "&amp;j_password=" + userPassword.getText().toString()).getBytes());
                          }
                      }, new ResponseExtractor&lt;String&gt;() {
                          @Override
                          public String extractData(ClientHttpResponse response) throws IOException {
                              List&lt;String&gt; cookies = response.getHeaders().get("Cookie");
                              if (cookies == null) {
                                  cookies = response.getHeaders().get("Set-Cookie");
                              }
                              String cookie = cookies.get(cookies.size() - 1);
                              int start = cookie.indexOf('=');
                              int end = cookie.indexOf(';');
  
                              return cookie.substring(start + 1, end);
                          }
                      });
              return null;
          }
  
          @Override
          protected void onPostExecute(String aVoid) {
              super.onPostExecute(aVoid);
          }
      }
  </code></pre>
  
  <p>Thread to retrieve images : </p>
  
  <pre><code> @Override
      public List&lt;RestImage&gt; getImagesForMenuCard(final int menuCardId) {
          final RestTemplate restTemplate = StaticRestTemplate.getRest();
  
          Thread thread = new Thread(new Runnable() {
              @Override
              public void run() {
                  HttpHeaders requestHeaders = new HttpHeaders();
                  requestHeaders.add("Cookie", "JSESSIONID=" + StaticRestTemplate.jsessionid);
                  requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
                  HttpEntity&lt;?&gt; requestEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
                  restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
                  menuCardImageEntity = restTemplate.exchange(menucardImageList + menuCardId, HttpMethod.GET, requestEntity, RestImage[].class);
              }
          });
          thread.start();
          try {
              thread.join();
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          RestImage[] restImages = menuCardImageEntity.getBody();
          List&lt;RestImage&gt; restImageList = new ArrayList&lt;&gt;();
          Collections.addAll(restImageList, restImages);
          return restImageList;
      }
  </code></pre>
  
  <p>I find the Thread model easy, as I know it better. I really don't understand the requirement to create a class to make a Network request. 
  Is my understanding of AsyncTask broken or this is the way it is supposed to be.</p>
  
  <p>Apologies for the copious amount of text, thought I would concisely explain my situation. Thanks a lot. </p>