Android 如何在异步任务下载程序中重试? 公共类PreviewDownload扩展异步任务{ 公共静态最终字符串TAG=“PreviewDownload”; 公共字符串inputPath=null; 公共字符串outputFolder=null; 公共IRIssue问题=空; @凌驾 受保护的字符串背景(字符串…参数){ InputStream输入=null; OutputStream输出=null; HttpURLConnection=null; issue=Broker.model.issueDataStore.getIRIssue(参数[0]); outputFolder=IRConstant.issueFolder(issue.year、issue.month、issue.day、issue.pubKey); 试一试{ inputPath=IRConstant.downloadFile(issue.year、issue.month、issue.day、issue.pubKey,“预览”,“0”); URL=新URL(输入路径); Log.d(标签,“输入:”+inputPath); connection=(HttpURLConnection)url.openConnection(); connection.connect(); if(connection.getResponseCode()!=HttpURLConnection.HTTP\u确定) 返回null; //return“Server returned HTTP”+connection.getResponseCode() //+“”+连接。getResponseMessage(); //下载该文件 输入=连接。getInputStream(); output=newfileoutputstream(outputFolder+“/preview.zip”); Log.d(标签,“输出:”+输出); 字节数据[]=新字节[1024]; 整数计数; 而((计数=输入。读取(数据))!=-1){ 输出.写入(数据,0,计数); } }捕获(例外e){ //返回e.toString(); 返回null; }最后{ 试一试{ if(输出!=null) output.close(); 如果(输入!=null) input.close(); }捕获(忽略IOException){ } if(连接!=null) 连接断开(); } 返回outputFolder; } @凌驾 受保护的void onPostExecute(字符串输出文件夹){ //TODO自动生成的方法存根 super.onPostExecute(outputFolder); if(outputFolder!=null){ File zipFile=新文件(outputFolder+“/preview.zip”); if(Utils.unzip(outputFolder,outputFolder+“/preview.zip”)){ zipFile.delete(); issue.isthumb=1; }否则{ issue.isthumb=0; } }否则{ Toast.makeText(Broker.launchectivity.getBaseContext(),R.string.wordCantDownload,Toast.LENGTH_LONG.show(); issue.isthumb=0; } issue.updateProgress(issue.progress); } }

Android 如何在异步任务下载程序中重试? 公共类PreviewDownload扩展异步任务{ 公共静态最终字符串TAG=“PreviewDownload”; 公共字符串inputPath=null; 公共字符串outputFolder=null; 公共IRIssue问题=空; @凌驾 受保护的字符串背景(字符串…参数){ InputStream输入=null; OutputStream输出=null; HttpURLConnection=null; issue=Broker.model.issueDataStore.getIRIssue(参数[0]); outputFolder=IRConstant.issueFolder(issue.year、issue.month、issue.day、issue.pubKey); 试一试{ inputPath=IRConstant.downloadFile(issue.year、issue.month、issue.day、issue.pubKey,“预览”,“0”); URL=新URL(输入路径); Log.d(标签,“输入:”+inputPath); connection=(HttpURLConnection)url.openConnection(); connection.connect(); if(connection.getResponseCode()!=HttpURLConnection.HTTP\u确定) 返回null; //return“Server returned HTTP”+connection.getResponseCode() //+“”+连接。getResponseMessage(); //下载该文件 输入=连接。getInputStream(); output=newfileoutputstream(outputFolder+“/preview.zip”); Log.d(标签,“输出:”+输出); 字节数据[]=新字节[1024]; 整数计数; 而((计数=输入。读取(数据))!=-1){ 输出.写入(数据,0,计数); } }捕获(例外e){ //返回e.toString(); 返回null; }最后{ 试一试{ if(输出!=null) output.close(); 如果(输入!=null) input.close(); }捕获(忽略IOException){ } if(连接!=null) 连接断开(); } 返回outputFolder; } @凌驾 受保护的void onPostExecute(字符串输出文件夹){ //TODO自动生成的方法存根 super.onPostExecute(outputFolder); if(outputFolder!=null){ File zipFile=新文件(outputFolder+“/preview.zip”); if(Utils.unzip(outputFolder,outputFolder+“/preview.zip”)){ zipFile.delete(); issue.isthumb=1; }否则{ issue.isthumb=0; } }否则{ Toast.makeText(Broker.launchectivity.getBaseContext(),R.string.wordCantDownload,Toast.LENGTH_LONG.show(); issue.isthumb=0; } issue.updateProgress(issue.progress); } },android,asynchronous,android-asynctask,download,Android,Asynchronous,Android Asynctask,Download,这是我实现的下载程序,问题是,当网络丢失时,输出变为null并显示错误消息,但是,如果我想在显示错误消息之前重试两次,有什么方法可以做到这一点?如果我希望不传入对象而不是字符串,是否不建议这样做?谢谢int expectedLength=connection.getContentLength(); 您能否与预期长度和下载长度进行比较并重试?在出现错误时,是什么阻止您从catch块重新实例化和重新执行“Downloader” 您可以在dowloader实例之间使用一个公共共享对象来计算尝试次数,

这是我实现的下载程序,问题是,当网络丢失时,输出变为null并显示错误消息,但是,如果我想在显示错误消息之前重试两次,有什么方法可以做到这一点?如果我希望传入对象而不是字符串,是否不建议这样做?谢谢

int expectedLength=connection.getContentLength();

您能否与预期长度和下载长度进行比较并重试?

在出现错误时,是什么阻止您从catch块重新实例化和重新执行“Downloader”


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

在深入研究这个方向之前,我可以请您阅读以下内容吗:?这意味着对于每个下载对象,我需要添加一个整数重试时间字段?是的。这就是重点。但实际上,请阅读我作为评论添加的另一个链接。
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);
    }
}