2015-10-23 58 views
0

我正在使用Ion library for Android https://github.com/koush/ion
我從服務器下載文件,當我通過不在服務器上的文件的url時,它仍然保存在SD卡上。要清楚,讓我們假設我有以下網址:https://example.com/img/img_3.jpg但沒有這樣的文件。所以實際上這個URL是404 Not Found,但是ion在我的SD卡上創建了一個文件img_3.jpg。當我打開圖像時,它是空白的。我試圖檢查下載的文件是否爲空,但事實並非如此。那麼是否有可能禁止從不存在的URL下載。
這裏是我的代碼:Ion Android和錯誤的URL

private void executeDownload(final FilesToDownload downloadFiles) { 
     if (downloading != null && !downloading.isCancelled()) { 
      resetDownload(); 
      return; 
     } 
     FileAndDirName fileAndDir = downloadFiles.popFileAndDirName(); 
     final int size = downloadFiles.getFilesAndDirSize(); 
     final String fileName = fileAndDir.getFileName(); 
     final String dirName = fileAndDir.getDirName(); 
     String url = mServerUrl + dirName + "/" + fileName; 

     File dir = new File(root.getAbsolutePath() + "/seatconnect/" + dirName); 
     if (!dir.exists()) { 
      dir.mkdirs(); 
     } 
     final File destinationFile = new File(dir, fileName); 

     downloading = Ion.with(getActivity()) 
       .load(url) 
         // attach the percentage report to a progress bar. 
         // can also attach to a ProgressDialog with progressDialog. 
       .progressBar(mProgressBar) 
       .progressDialog(mProgressDialog) 
         // callbacks on progress can happen on the UI thread 
         // via progressHandler. This is useful if you need to update a TextView. 
         // Updates to TextViews MUST happen on the UI thread. 
       .progressHandler(new ProgressCallback() { 
        @Override 
        public void onProgress(long downloaded, long total) { 
//      mProgressDialog.setProgress((int) downloaded); 
        } 
       }) 
         // write to a file 
       .write(destinationFile) 
         // run a callback on completion 
       .setCallback(new FutureCallback<File>() { 
        @Override 
        public void onCompleted(Exception e, File result) { 
         resetDownload(); 
         if (e != null) { 
          Toast.makeText(getActivity(), "Error downloading file " + fileName, Toast.LENGTH_SHORT).show(); 
//       return; 
         } else { 
          Toast.makeText(getActivity(), "File download complete " + fileName, Toast.LENGTH_SHORT).show(); 
         } 

         if (result.exists() && result.length() == 0) { 
          String message = result.delete() ? "Deleted empty file " : "The file is not empty "; 
          message += fileName; 
          Log.d(TAG, message); 
         } 

         if (size != 0) { 
          mProgressDialog.show(); 
          executeDownload(downloadFiles); 
          return; 
         } 

         mProgressBar.setVisibility(View.INVISIBLE); 
         if (mProgressDialog.isShowing()) { 
          mProgressDialog.dismiss(); 
         } 
         mNewsFooterCheckForUpdateButton.setVisibility(View.VISIBLE); 
         mNewsFooterUpdateButton.setVisibility(View.INVISIBLE); 
        } 
       }); 
} 

回答

0

也許爲時已晚,但我認爲你可以通過使用withResponse()基本上嵌入了一個com.koushikdutta.ion.Response對象,這當然包含頭中下載的文件處理。然後你可以檢查它們以確保沒有返回錯誤代碼。

downloading = Ion.with(getActivity()) 
      .load(url) 
      .progressBar(mProgressBar) 
      .write(destinationFile) 
      .withResponse() // response will incapsulates the result file 
      .setCallback(new FutureCallback<Response<File>>() 
       @Override 
       public void onCompleted(Exception e, Response<File> response) { 
        File result = null; 
        if (e != null || response.getHeaders().code() != 200)) { // check response headers 
         // an error was occurred 
         // ... 
        } else { 
         // file was successfully downloaded 
         result = response.getResult(); 
         // ... 
        } 
      });