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开发者_如何学JAVA";
}
}
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.
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");
精彩评论