且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 android 4.4.2 版本上获取 net::ERR_CONNECTION_REFUSED (http://localhost:8080)

更新时间:2022-01-04 02:46:24

您的问题是由 cordova-plugin-ionic-webview 插件引起的,该插件是每个新的或更新的 Ionic 应用程序的一部分.

Your problem is caused by the cordova-plugin-ionic-webview plugin that is part of every new or updated Ionic app.

这曾经仅适用于 iOS,它用 WKWebView 替换了 UIWebView,但在 2018 年 7 月 23 日,他们发布了插件的 2.0 版,其中还包括对 Android 上使用的 webview 的更改.

This used to apply only to iOS, where it replaced the UIWebView with WKWebView, but on July 23rd 2018 they released version 2.0 of the plugin, that also included changes to the webview used on Android.

Android webview 现在使用位于 localhost:8080 的本地网络服务器来显示您的应用程序,而不是直接从文件系统请求文件.

The Android webview now uses a local webserver at localhost:8080 to show your app instead of requesting the files directly from the file system.

不幸的是,此更改还在文档中包含了这一点:

Unfortunately this change also included this bit in the documentation:

Requirements
- […]
- Android: Android 5.0+ and cordova-android 6.4+

所以 cordova-plugin-ionic-webview 只是不再支持 5.0 之前的 Android,这当然意味着您的应用将无法在 Android 4.x 上运行.

So cordova-plugin-ionic-webview just doesn’t support Android earlier than 5.0 any more, which of course means your app will not work on Android 4.x.

一种解决方案是将插件降级到支持 Android 4.x 的最新版本:

One solution is to downgrade the plugin to the last version that supported Android 4.x:

ionic cordova plugin add cordova-plugin-ionic-webview@1.2.1

更详细的信息和替代解决方案:
https://ionic.zone/debug/ionic-and-android-4

More elaborate information and alternative solutions:
https://ionic.zone/debug/ionic-and-android-4