简体   繁体   English

离子Android和错误的URL

[英]Ion Android and wrong URL

I'm using Ion library for Android https://github.com/koush/ion 我正在为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. 我从服务器下载文件,并且当我传递不在服务器上的文件的URL时,它还是保存在SD卡中。 To be clear, let's assume I have following url: https://example.com/img/img_3.jpg but there is no such file. 为了清楚起见,假设我有以下URL: https : //example.com/img/img_3.jpg但没有这样的文件。 So actually this url is 404 Not Found, but ion creates a file img_3.jpg on my SD card. 因此,实际上该URL是404 Not Found,但是ion在我的SD卡上创建了一个文件img_3.jpg。 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. 因此有可能禁止从不存在的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. 也许为时已晚,但我认为您可以使用withResponse()来处理它,该方法基本上将下载的文件嵌入com.koushikdutta.ion.Response对象中,该对象当然包含标头。 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();
                        // ...
                    }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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