首页 > 解决方案 > 如果服务器返回错误,则使用 rn-fetch-blob 的 React Native 应用程序崩溃

问题描述

我正在尝试学习 React Native,并且在此过程中我正在尝试使用以下代码使用 rn-fetch-blob 下载文件

        var url = APIPREFIX + '/image/download/' + image.id;
        var config = {
            path: ((Platform.OS === 'ios') ? RNFetchBlob.fs.dirs.DocumentDir : RNFetchBlob.fs.dirs.DownloadDir) + '/' + image.fileName,
        };
    
        RNFetchBlob.config(config)
            .fetch('GET', url)
            .then((res) => {
                    var path = res.path();
                    
                    if (Platform.OS === 'ios') {
                        RNFetchBlob.ios.previewDocument(path)
                    } else {
                        RNFetchBlob.android.actionViewIntent(path, image.image ? "image/png" : image.type);
                    }
            }).catch((errorMessage) => {
                    console.log(errorMessage);
                    // no special error handling required.
                    // added this line to check if an error is being returned
                });

当服务器返回一个文件(它总是一个图像文件)时,这可以正常工作。但是,如果服务器返回任何错误(通常为 404 或 401)- 应用程序崩溃。

adb logcat 显示以下几行

05-05 22:40:09.984   518  2945 I system_server: oneway function results will be dropped but finished with status OK and parcel size 4
05-05 22:40:10.038  8846  9054 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
05-05 22:40:10.038  8846  9054 E AndroidRuntime: Process: com.downloadTest, PID: 8846
05-05 22:40:10.038  8846  9054 E AndroidRuntime: java.lang.ClassCastException: okhttp3.internal.http.RealResponseBody cannot be cast to com.RNFetchBlob.Response.RNFetchBlobFileResp
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobReq.done(RNFetchBlobReq.java:594)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobReq.access$100(RNFetchBlobReq.java:72)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobReq$3.onResponse(RNFetchBlobReq.java:493)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
05-05 22:40:10.038  8846  9054 E AndroidRuntime:        at java.lang.Thread.run(Thread.java:923)
05-05 22:40:10.041   518  2945 W ActivityTaskManager:   Force finishing activity com.downloadTest/.MainActivity
"react": "16.13.1"
"react-native": "^0.64.0"
"rn-fetch-blob": "^0.12.0"
Android Version: 10
Android Simulator Version: 11

Android 模拟器/Android 设备/iOS 模拟器/iPhone 设备的行为相同。

这里的一些文章谈论的是 0.12.1 版本,但我无法找到如何下载它 - 以及它是否相关。 在一些答案中,它谈到了更改 AndroidManifest 以允许访问文件系统 - 但这似乎不是问题

我请求帮助以了解如何在我的代码中解决此问题或处理此问题。

标签: react-nativern-fetch-blob

解决方案


推荐阅读