【问题标题】:How to refer to a result of AsyncTask in another class?如何在另一个类中引用 AsyncTask 的结果?
【发布时间】:2016-06-27 20:05:25
【问题描述】:

我有 2 个类:FeedItem 和 imgDownloader。在第二个中,我将 url 抓取到图像中,在第一个中,我正在创建一个 setLink 方法,以在我的应用程序中分配它并通过 Picasso 显示。

feedItem.class 的片段:

public class feedItem {
    String link;

public void setLink(String link) {

    imgDownload task = new imgDownload();
    String urlx = task...;  //Here should be a result of that AsyncTask's doInBackground
    this.link = urlx;
}
}

imgDownload.class:

public class imgDownload extends AsyncTask<String, String, String> {

    feedItem item = new feedItem();
    String url = item.getLink();
    String imglink;

    @Override
    protected String doInBackground(String... params) {
        Document doc = null;
        try {
            doc = Jsoup.connect(url).get();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        Element masthead = doc.select("div.post-image").select("img[src~=(?i)\\.(png|jpe?g)]").first();
        url = masthead.absUrl("src");
        return url;
    }
    @Override
    protected void onPostExecute(String result) {
        imglink = result; //Result of doInBackground
    }
}

我阅读了几乎所有与此类似的问题,但经过 2 天的学习后,我仍然不知道如何正确地做到这一点,所以我需要一个具体的答案如何做到这一点。

//////////编辑

所以我使用了那个回调,我得到了各种各样的错误,比如:

                                                                 --------- beginning of crash
06-27 23:57:14.446 3274-3321/pl.dariusz.app E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
                                                            Process: pl.dariusz.app, PID: 3274
                                                            java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                at java.lang.Thread.run(Thread.java:818)
                                                             Caused by: java.lang.IllegalArgumentException: Must supply a valid URL
                                                                at org.jsoup.helper.Validate.notEmpty(Validate.java:102)
                                                                at org.jsoup.helper.HttpConnection.url(HttpConnection.java:72)
                                                                at org.jsoup.helper.HttpConnection.connect(HttpConnection.java:36)
                                                                at org.jsoup.Jsoup.connect(Jsoup.java:73)
                                                                at pl.dariusz.app.imgDownloader.doInBackground(imgDownloader.java:30)
                                                                at pl.dariusz.app.imgDownloader.doInBackground(imgDownloader.java:12)
                                                                at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                at java.lang.Thread.run(Thread.java:818) 

06-27 23:57:14.533 3274-3302/pl.dariusz.app I/Process:发送信号。 PID:3274 SIG:9

【问题讨论】:

  • 你可以实现一个接口并在task的构造函数中设置它

标签: java android android-asynctask


【解决方案1】:

您需要创建一个回调,您的 AsyncTask 可以使用该回调将数据发送到其调用 Activity。

一个可以从 AsyncTask 获取字符串的简单接口就足够了。

您可以在我的Learn Android 存储库中找到此方法传递图像的一个非常简单的示例。

【讨论】:

    【解决方案2】:

    为异步任务添加构造函数,参数为您想要的属性。类似的东西:

    public class ImgDownload extends AsyncTask<String, String, String> {
    
        feedItem item = new feedItem();
        String url = item.getLink();
        String imglink;
    
        public ImgDownload(String link){
             imglink = link;
        } 
    
        @Override
        protected String doInBackground(String... params) {
            Document doc = null;
            try {
                doc = Jsoup.connect(params[0]).get();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            Element masthead = doc.select("div.post-image").select("img[src~=(?i)\\.(png|jpe?g)]").first();
            url = masthead.absUrl("src");
            return url;
        }
        @Override
        protected void onPostExecute(String result) {
            imglink = result; //Result of doInBackground
        }
    }
    

    并像这样调用这个异步任务:

    public class feedItem {
        String mLink;
    
        public void setLink(String rawLink) {
            imgDownload task = new imgDownload(mLink);
            task.execute(rawLink);
        }
    }
    

    更多:Т他的解决方案是丑陋的。它的工作原理,但它不灵活。您可以创建一个可用于回调的接口。看看here。它完全是正确的方式。

    【讨论】:

    • 我使用了您链接中的那个方法,但它不起作用(查看我的帖子的编辑)。
    【解决方案3】:
     imgDownload task = new imgDownload();
     String result = task.execute().get; // result is the output from onPostExecute.
    

    【讨论】:

      【解决方案4】:

      我在这里写了一份完整的操作指南:https://stackoverflow.com/a/36166643/3962091

      一般的过程是将你的AsyncTask放在它自己的类文件中,并在AsyncTask中声明一个回调接口,你将向它发送结果。然后任何其他类(包括Activity)都可以简单地实现回调接口以获得AsyncTask 必须提供的任何结果。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多