2013-12-19 28 views
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); 
    } 
} 

這是我實現的下載器,問題是,當網絡丟失,輸出變爲空並顯示錯誤消息,但是,如果我想在顯示錯誤消息之前重試兩次,有沒有什麼辦法做這個?如果我認爲而不是傳遞一個對象而不是字符串,是不是推薦?謝謝如何在異步任務下載器中重試?

+2

在深入研究這個方向之前,我可以請您閱讀以下內容:http://stackoverflow.com/a/13082084/693752? – Snicolas

回答

1

什麼能夠防止您在發生錯誤時從您的catch塊重新實例化並重新執行「Downloader」?

您可以在下載器實例之間使用一個公共共享對象來計算嘗試次數,或者更好地將參數傳遞給它們中的每一個。在catch塊中,如果你沒有達到極限,你會再試一次,並增加傳遞給新的下載器的值......遞歸的東西。

+0

這意味着我需要添加一個整數重試時間字段的每個下載對象? – user1871516

+0

是的。這纔是重點。但是,真的,請閱讀我作爲評論添加的其他鏈接。 – Snicolas

1

int expectedLength = connection.getContentLength(); 你能比較預期的長度&下載長度和重試?