我下载文件的代码如下
RNFetchBlob
.config({
path : path,
background: true,
cacheable:false,
timeout: 1000 * 60 * 15 //15 minutes
})
.fetch('GET', forFile.url, {
//some headers ..
})
.then((res) => {
Realm.open({schema: [ScheduleSchema,PlaylistSchema,FileSchema],schemaVersion: schema_version})
.then(realm => {
realm.write(() => {
localPath = Platform.OS === 'android' ? 'file://' + res.path() : `playlists/${playlistName}/${getFileName(forFile)}`
forFile.file_local_path = localPath
console.log('The file saved to ', res.path() + 'for remote url' +forFile.url)
})
}).catch((error) => {
alert("realm open error on file download service", error);
console.log(error);
reject(error)
})
resolve(forFile);
}).catch((error)=>{
console.log("file download error " + error);
showToastMessage(I18n.t("ErrorMessage.ErrorInFileDownload"))
})
此代码适用于android 7,但不适用于android 9。
Android Studio Logcat出现以下错误
java.lang.IllegalStateException: cannot make a new request because the previous response is still open: please call response.close()
at okhttp3.internal.connection.Transmitter.newExchange(Transmitter.java:164)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:41)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at com.RNFetchBlob.RNFetchBlobReq$2.intercept(RNFetchBlobReq.java:385)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:221)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:172)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)