Android Asynchronous calls

Im making an asynchronous call from a class to another.

Here is my actual code:

public class HttpRequestHelper extends AsyncTask
{

  @Override
  protected Object doInBackground(Object... params) 
  {
   try 
   {
      // Create a URL for the desired page
      URL url = new URL("http://www.google.com");
      // Read all the text returned by the server
      BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
      String str;
      while ((str = in.readLine()) != null) 
      {
      }
      in.close();
    } 
    catch (MalformedURLException e) 
    {

    }    
    return null;
   }
   protected void onPostExecute(Long result)
   {
        String hello="hello world";   
   }
}

Im using the class above from another class using:

HttpRequestHelper helper=new HttpRequestHelper();
helper.execute("whatever");

But after execute, i dont know when the execution is finished, how could i suscribe to the callback of the asynchronous operation?

Thanks in advance.

Best Regards. Josema.

Answers


You should have a look at Handlers as well; the documentation always refers to their use with regards to different threads passing messages to each other, so assuming your server and client classes are running in different threads, it should be appropriate. Here's a simple tutorial on using handlers in the context of having a loading thread and a dialog thread so you should be able to adapt it to your needs. The code you want is hidden under the "Example ProgressDialog with a second thread" about 2/3rds down the page.


Sounds to me you should use BroadcastReceiver to pick up sendBroadcast()


You can do something like this:

HttpRequestHelper helper = new HttpRequestHelper(){
   protected void onPostExecute(Long result)
   {
        super.onPostExecute(result);
        //do what ever you want to do


   }
};

helper.execute("whatever");

Need Your Help

Problems with TestFlightApp

objective-c ios testflight

I'm developing iOS apps and I'd like to share my apps with TestFlight testers. This is not the problem and works fine (via email). However, when I'm uploading a new build, I'd like to have in-app u...

Lyrics api for my iOS music app

ios api ios4 audio-streaming

I would like to integrate music lyrics in my iOS app. Do you have a preference choice for an APi?