问题描述
我尝试使用 epubjs-rn 软件包通过以下代码显示我的epub,在调试版本中一切正常,当我进行发布版本和测试时,epub书不显示并修复加载开始显示, 我在android studio控制台中调试并发现相同的错误:
I/ReactNativeJS: readBookEpub id: 4868
I/ReactNativeJS: source: http://localhost:8899/4868/
E/ReactNativeJS: Empty Response
我的代码:
_showEpub(bookName) {
console.log("_showEpub");
const targetPath = 'http://127.0.0.1:8899/' + bookName + '.epub';
//------------------------------------------------------show Epub
try {
this.streamer.start()
.then((origin) => {
console.log("Served from:",origin);
// this.streamer.check('http://127.0.0.1:8899/moby-dick.epub').then((t) => console.log("hiii"+t));
return this.streamer.get(targetPath);
})
.then((src) => {
console.log("Loading from:",src);
return this.setState({ urlBook: src });
});
}
catch (err) {
console.log("Error: " + err);
}
//---------------------------------------------------
}
用于展示图书,我通过RNBackgroundDownloader
从服务器下载图书并保存,然后将其解压缩到素材资源文件夹中以供本地阅读,
我的解压缩功能:
_unZipBook(bookName) {
console.log("_unZipBook")
const sourcePath = Dirs.DocumentDir + '/FaraSource/e/' + bookName + '.zip';
const targetPath = `${Dirs.DocumentDir}/assets/${bookName}`;
unzip(sourcePath,targetPath)
.then((path) => {
console.log("unZip " + bookName + "in" + targetPath);
this._showEpub(bookName);
// return url;
})
}
有人可以帮助我解决这个问题吗?
解决方法
我解决了这个问题,必须在AndroidManifest中添加一行
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>