首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重复AsyncTask

重复AsyncTask
EN

Stack Overflow用户
提问于 2013-08-21 13:40:36
回答 3查看 7.3K关注 0票数 6

我对在安卓应用程序中重复AsyncTask的可能性表示怀疑。我想重复一些操作,从服务器下载一个文件,例如,n次,如果由于某些原因无法下载该文件。有一种快速的方法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-21 13:44:41

不能重复AsyncTask ,但可以重复它执行的操作。

我已经创建了一个小助手类,您可能希望扩展这个类来代替AsyncTask,唯一的区别是您将使用repeatInBackground而不是doInBackground,并且onPostExecute将有一个新的参数,最终抛出的异常。

repeatInBackground中的任何内容都将被自动重复,直到结果与空/异常不同,并且不会抛出,并且比maxTries少。

循环中抛出的最后一个异常将在onPostExecute中返回(结果,异常)。

可以使用RepeatableAsyncTask(int重试)构造函数设置最大尝试。

代码语言:javascript
复制
public abstract class RepeatableAsyncTask<A, B, C> extends AsyncTask<A, B, C> {
    private static final String TAG = "RepeatableAsyncTask";
    public static final int DEFAULT_MAX_RETRY = 5;

    private int mMaxRetries = DEFAULT_MAX_RETRY;
    private Exception mException = null;

    /**
     * Default constructor
     */
    public RepeatableAsyncTask() {
        super();
    }

    /**
     * Constructs an AsyncTask that will repeate itself for max Retries
     * @param retries Max Retries.
     */
    public RepeatableAsyncTask(int retries) {
        super();
        mMaxRetries = retries;
    }

    /**
     * Will be repeated for max retries while the result is null or an exception is thrown.
     * @param inputs Same as AsyncTask's
     * @return Same as AsyncTask's
     */
    protected abstract C repeatInBackground(A...inputs);

    @Override
    protected final C doInBackground(A...inputs) {
        int tries = 0;
        C result = null;

        /* This is the main loop, repeatInBackground will be repeated until result will not be null */
        while(tries++ < mMaxRetries && result == null) {
            try {
                result = repeatInBackground(inputs);
            } catch (Exception exception) {
                /* You might want to log the exception everytime, do it here. */
                mException = exception;
            }
        }
        return result;
    }

    /**
     * Like onPostExecute but will return an eventual Exception
     * @param c Result same as AsyncTask
     * @param exception Exception thrown in the loop, even if the result is not null.
     */
    protected abstract void onPostExecute(C c, Exception exception);

    @Override
    protected final void onPostExecute(C c) {
        super.onPostExecute(c);
        onPostExecute(c, mException);
    }
}
票数 11
EN

Stack Overflow用户

发布于 2013-08-21 13:45:24

不能重用与根据AsyncTask文档相同的根据AsyncTask文档对象

任务只能执行一次(如果尝试第二次执行,将引发异常)。

但是,您可以在循环中创建这个类的多少个新对象。但是,更好的方法是在doInBackground()中执行多次下载操作。

如果这不能回答你的问题,那么请对你的问题更具体一些。

票数 1
EN

Stack Overflow用户

发布于 2015-06-09 08:32:58

我就是那样做的。它可以尝试和尝试,直到(尝试== MAX_RETRY)或结果为非空。一个从接受的答案稍微修改的代码,对我更好。

代码语言:javascript
复制
private class RssReaderTask extends AsyncTask<String, Void, ArrayList<RssItem>> {

    // max number of tries when something is wrong
    private static final int MAX_RETRY = 3;

    @Override
    protected ArrayList<RssItem> doInBackground(String... params) {

        ArrayList<RssItem> result = null;
        int tries = 0;

        while(tries++ < MAX_RETRY && result == null) {
            try {
                Log.i("RssReaderTask", "********** doInBackground: Processing... Trial: " + tries);
                URL url = new URL(params[0]);
                RssFeed feed = RssReader.read(url);
                result = feed.getRssItems();
            } catch (Exception ex) {
                Log.i("RssReaderTask", "********** doInBackground: Feed error!");
            }
        }

        return result;
    }

    @Override
    protected void onPostExecute(ArrayList<RssItem> result) {
        // deal with result
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18359039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档