0
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);
    }
}

Here is the downloader I implemented , the problem is , when the network lost, the output become null and show error message, however, if I would like to retry two times before showing error message, are there any way to do this? If I perfer not to pass in an object instead of string ,is it not recommended? thanks

user1871516
  • 979
  • 6
  • 14
  • 26
  • 2
    Before going to deep in that direction, may I invite you to read this : http://stackoverflow.com/a/13082084/693752 ? – Snicolas Dec 19 '13 at 16:41

2 Answers2

1

int expectedLength = connection.getContentLength(); can you compare with the expectedLength & downloaded length and retry?

Sheshu
  • 21
  • 3
1

What prevents you from re-instanciating and re-executing a "Downloader" from your catch blocks in case of errors ?

You could use a single common shared object between dowloader instances to count the attempts, or better, pass a parameter to each of them. In the catch block, you would then retry if you didn't reach the limit, and increase the value passed to a new downloader... Something recursive.

Snicolas
  • 37,840
  • 15
  • 114
  • 173