将 html 从 AsyncTask 对象传递回调用 Activity

发布于 2024-11-14 14:00:25 字数 367 浏览 2 评论 0原文

我有一个活动对象 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

来世叙缘 2024-11-21 14:00:25

好的,更多的谷歌搜索得出了以下解决方案。

myAct 需要一个接口

   public interface WebServiceListener 
{
    public void onHTTPGetComplete (ArrayList<String> arrayList);
}
public void onHTTPGetComplete  (ArrayList<String> arrayList) {
      String Response = arrayList.get(0);
      Toast worked = Toast.makeText(mainPage.this, Response, Toast.LENGTH_LONG);
      worked.show();
}

,并且 onPostExecute 中的 Async 需要调用它

listener.onHTTPGetComplete(myList);

OK, so a bit more googling came up with the following solution.

myAct needed an interface

   public interface WebServiceListener 
{
    public void onHTTPGetComplete (ArrayList<String> arrayList);
}
public void onHTTPGetComplete  (ArrayList<String> arrayList) {
      String Response = arrayList.get(0);
      Toast worked = Toast.makeText(mainPage.this, Response, Toast.LENGTH_LONG);
      worked.show();
}

and the Async in the onPostExecute needed to call it

listener.onHTTPGetComplete (myList);

丿*梦醉红颜 2024-11-21 14:00:25

使用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.

逆流 2024-11-21 14:00:25

从 AsyncTask 传回一个包含 HTML 的包。例子:

@Override
protected Bundle doInBackground(String... params) {

    Bundle b = new Bundle();

    HttpGet get = new HttpGet("myurl");

    HttpResponse response = HttpManager.execute(get);
    HttpEntity entity = response.getEntity();
    String raw_result = EntityUtils.toString(entity);
    entity.consumeContent();

    b.putString(IService.RESULT, raw_result);
    b.putInt(IService.STATUS, IService.STATUS_FINISHED);

    return b;

}


// override this in your activity as an anon inner class
@Override
protected void onPostExecute(Bundle b) {
    int resultCode = b.getInt(IService.STATUS);
}

Pass back a bundle from your AsyncTask with the HTML in it. Example:

@Override
protected Bundle doInBackground(String... params) {

    Bundle b = new Bundle();

    HttpGet get = new HttpGet("myurl");

    HttpResponse response = HttpManager.execute(get);
    HttpEntity entity = response.getEntity();
    String raw_result = EntityUtils.toString(entity);
    entity.consumeContent();

    b.putString(IService.RESULT, raw_result);
    b.putInt(IService.STATUS, IService.STATUS_FINISHED);

    return b;

}


// override this in your activity as an anon inner class
@Override
protected void onPostExecute(Bundle b) {
    int resultCode = b.getInt(IService.STATUS);
}
随遇而安 2024-11-21 14:00:25

我的方法是将一个抽象消费者方法添加到我的 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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文