我正在从一个类到另一个类进行异步调用。
这是我的实际代码:
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";
}
}
我从另一个类使用上面的类:
HttpRequestHelper helper=new HttpRequestHelper();
helper.execute("whatever");
但是执行完后,不知道什么时候执行完毕,怎么订阅异步操作的回调呢?
提前致谢。
最好的问候。 何塞玛。
最佳答案
你应该看看Handlers还有;文档总是提到它们在不同线程相互传递消息方面的使用,因此假设您的服务器和客户端类在不同线程中运行,它应该是合适的。这是一个 simple tutorial关于在具有加载线程和对话线程的上下文中使用处理程序,因此您应该能够根据您的需要进行调整。您想要的代码隐藏在页面下方大约 2/3 的“Example ProgressDialog with a second thread”下。
关于安卓异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2447897/