将 html 从 AsyncTask 对象传递回调用 Activity
我有一个活动对象 myAct。 myAct 创建并调用我的 httpGetter 对象,该对象扩展了 AsyncTask
httpGetter 做得很好!我的进度条效果很好。它获取 HTML 数据。我在 httpGetter.onPostExecute 中放了一个祝酒词......这是 HTML!!!!
如何将该数据从 onPostExecute 传递回活动?我什至不知道该用谷歌搜索什么。我尝试了一些方法,我看到的所有示例要么更新了 textView,要么只是在 onPostExecute 中的 toast 中显示数据。我想在调用活动中恢复一切。在我的 httpGetter 类中,我创建了一个方法来设置调用 Activity 的句柄,认为我可以使用结果字符串调用方法。运气不好。
I have an activity object, myAct. myAct creates and calls my httpGetter object which extends AsyncTask
httpGetter does a great job! My progress bars work great. It gets the HTML data. I put a toast in the httpGetter.onPostExecute..... There's the HTML!!!!
How do I pass that data back to the activity from the onPostExecute? I don't even know what to google for this. I tried a few things, and all of the examples I saw either updated a textView or just showed the data in a toast, right in the onPostExecute. I want to resume things back in the calling Activity. In my httpGetter class, I created a method to set a handle to the calling Activity thinking I could call a method with the result String. No luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,更多的谷歌搜索得出了以下解决方案。
myAct 需要一个接口
,并且 onPostExecute 中的 Async 需要调用它
OK, so a bit more googling came up with the following solution.
myAct needed an interface
and the Async in the onPostExecute needed to call it
使用intent.putExtra() 将意图从httpGetter 发送到myAct 以传回数据怎么样?为此,您需要在创建 AsyncTask 时让 httpGetter 了解 myAct 的上下文。
How about sending an intent from httpGetter to myAct with intent.putExtra() to pass back your data? For this to work, you'll need to let httpGetter know about myAct's context when you create the AsyncTask.
从 AsyncTask 传回一个包含 HTML 的包。例子:
Pass back a bundle from your AsyncTask with the HTML in it. Example:
我的方法是将一个抽象消费者方法添加到我的 HttpAsyncTask 中,我会将服务器从 onPostExecute 返回的数据传递给该方法。现在调用它的活动需要实现消费者方法,使活动能够访问数据:-)
The way I did it was add an abstract consumer method to my HttpAsyncTask to which I would pass the data the server returned from onPostExecute. Now the activity calling it needs to implement the consumer method , giving the activity access to the data :-)