简体   繁体   中英

Ion Android and wrong URL

I'm using Ion library for Android https://github.com/koush/ion
I download files from server and when I pass the url for file that is not on the server it is saved on sd card anyway. To be clear, let's assume I have following url: https://example.com/img/img_3.jpg but there is no such file. So actually this url is 404 Not Found, but ion creates a file img_3.jpg on my SD card. When I open the image it is blank. I've tried to check if downloaded file is empty, but it's not. So is there any possibility to forbid ion to download from not existing URL.
Here is my code:

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

Maybe too late but I think you can handle it by using withResponse() which basically embeds the downloaded file within a com.koushikdutta.ion.Response object, which of course contains headers. Then you can check them to be sure that no error code was returned.

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();
                        // ...
                    }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM