【问题标题】:How to retry in the async task downloader?如何在异步任务下载器中重试?
【发布时间】:2013-12-19 16:33:32
【问题描述】:
public class PreviewDownload extends AsyncTask<String, Void, String> {
    public static final String TAG = "PreviewDownload";
    public String inputPath = null;
    public String outputFolder = null;
    public IRIssue issue = null;

    @Override
    protected String doInBackground(String... parms) {
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        issue =  Broker.model.issueDataStore.getIRIssue(parms[0]);
        outputFolder = IRConstant.issueFolder(issue.year, issue.month, issue.day, issue.pubKey);

        try {
            inputPath = IRConstant.downloadFile(issue.year, issue.month, issue.day, issue.pubKey, "preview", "0");
            URL url = new URL(inputPath);

            Log.d (TAG,"input: " + inputPath);

            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
                return null;
            // return "Server returned HTTP " + connection.getResponseCode()
            // + " " + connection.getResponseMessage();

            // download the file
            input = connection.getInputStream();
            output = new FileOutputStream(outputFolder + "/preview.zip");

            Log.d (TAG,"output: " + output);

            byte data[] = new byte[1024];
            int count;
            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            // return e.toString();
            return null;
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();

        }
        return outputFolder;
    }

    @Override
    protected void onPostExecute(String outputFolder) {
        // TODO Auto-generated method stub
        super.onPostExecute(outputFolder);
        if (outputFolder != null) {
            File zipFile = new File (outputFolder + "/preview.zip");
            if (Utils.unzip(outputFolder,outputFolder + "/preview.zip" )) {
                zipFile.delete();
                issue.isThumbDownloaded = 1;
            } else {
                issue.isThumbDownloaded = 0;
            }
        } else {
            Toast.makeText(Broker.launcherActivity.getBaseContext(), R.string.wordCantDownload, Toast.LENGTH_LONG).show();
            issue.isThumbDownloaded = 0;
        }
        issue.updateProgress(issue.progress);
    }
}

这是我实现的下载器,问题是,当网络丢失时,输出变为空并显示错误消息,但是,如果我想在显示错误消息之前重试两次,有什么办法可以做到这一点?如果我更喜欢 not 传递对象而不是字符串,是否不推荐?谢谢

【问题讨论】:

标签: android asynchronous android-asynctask download


【解决方案1】:

发生错误时,是什么阻止您从 catch 块中重新实例化和重新执行“下载器”?

您可以在下载程序实例之间使用单个公共共享对象来计算尝试次数,或者更好地为每个实例传递一个参数。在 catch 块中,如果未达到限制,您将重试,并增加传递给新下载器的值...递归。

【讨论】:

  • 这意味着我需要为每个下载对象添加一个整数重试次数字段?
  • 是的。这才是重点。但实际上,请阅读我作为评论添加的另一个链接。
【解决方案2】:

int expectedLength = connection.getContentLength(); 你能与预期长度和下载长度进行比较并重试吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 2020-03-30
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多