Ionic 应用程序在浏览器和 ios 上运行,但不在 android 上,与服务器的连接不成功。 (文件///android_asset/www/index.html)在android上

Posted

技术标签:

【中文标题】Ionic 应用程序在浏览器和 ios 上运行,但不在 android 上,与服务器的连接不成功。 (文件///android_asset/www/index.html)在android上【英文标题】:Ionic app runs on browser and ios but not on android, the connection to the server was unsuccessful. (file ///android_asset/www/index.html) on android 【发布时间】:2021-06-28 03:55:36 【问题描述】:

我的 ionic 应用程序之前可以在所有平台上运行,即 androidios,也可以在具有 ionic serve 的浏览器上运行。

但是在最近一次更新后,当我尝试添加 android 平台时,我正在构建应用程序没有问题,但是当我在模拟器或真实设备上运行应用程序时,它只是在启动屏幕后显示一个空白屏幕。

在模拟器上,它显示一条消息,因为与服务器的连接不成功。 (文件 ///android_asset/www/index.html

但是,相同的应用程序在 ios 设备、ios 模拟器和 ionic serve 上成功运行,没有任何问题。

尝试了网上列出的一些解决方案,例如,

    <preference name="loadUrlTimeoutValue" value="700000" /> android:usesCleartextTraffic="true" on androidmanifest.xml
    <?xml version="1.0" encoding="utf-8"?>
        <network-security-config>
            <domain-config cleartextTrafficPermitted="true">
                <domain includeSubdomains="true">localhost</domain>
                <domain includeSubdomains="true">example.com</domain>
            </domain-config>
        </network-security-config>

在 network_security_config.xml 上 example.com 被我的域替换。

但上述所有解决方案均无效。

一开始我以为是cordova-android@9版本的问题,尝试切换回以前的版本,但还是不行。

这是我的离子信息

离子:

Ionic CLI:6.13.1 (/opt/homebrew/lib/node_modules/@ionic/cli) 离子框架:@ionic/angular 5.5.4 @angular-devkit/build-angular:0.803.29 @angular-devkit/原理图:8.3.29 @角/cli:8.3.29 @ionic/angular-toolkit:2.1.2

科尔多瓦:

科尔多瓦 CLI:10.0.0 Cordova 平台:安卓 9.0.0 Cordova 插件:cordova-plugin-ionic-keyboard 2.2.0、cordova-plugin-ionic-webview 5.0.0(和其他 19 个插件)

实用程序:

cordova-res(可用更新:0.15.3):0.15.1 本机运行:1.3.0

系统:

Android SDK 工具:26.1.1 (/Users/rameshramgopal/Library/Android/sdk) ios部署:1.11.3 ios-sim : 8.0.2 NodeJS:v15.6.0(/opt/homebrew/Cellar/node/15.6.0/bin/node) npm:7.7.6 操作系统:macOS 大苏尔 Xcode:Xcode 12.3 构建版本 12C33

这是我的 package.json

    
      "name": "woocommerce",
      "version": "0.0.1",
      "author": "Ionic Framework",
      "homepage": "https://ionicframework.com/",
      "scripts": 
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      ,
      "private": true,
      "dependencies": 
        "@angular/common": "~8.2.14",
        "@angular/core": "~8.2.14",
        "@angular/forms": "~8.2.14",
        "@angular/http": "^7.2.16",
        "@angular/platform-browser": "~8.2.14",
        "@angular/platform-browser-dynamic": "~8.2.14",
        "@angular/router": "~8.2.14",
        "@ionic-native/android-permissions": "^5.26.0",
        "@ionic-native/app-minimize": "^5.21.5",
        "@ionic-native/app-rate": "^5.21.5",
        "@ionic-native/core": "^5.0.0",
        "@ionic-native/crop": "^5.21.6",
        "@ionic-native/email-composer": "^5.21.5",
        "@ionic-native/facebook": "^5.23.0",
        "@ionic-native/file-transfer": "^5.21.6",
        "@ionic-native/geolocation": "^5.26.0",
        "@ionic-native/google-plus": "^5.24.0",
        "@ionic-native/http": "^5.21.5",
        "@ionic-native/image-picker": "^5.23.0",
        "@ionic-native/in-app-browser": "^5.23.0",
        "@ionic-native/location-accuracy": "^5.26.0",
        "@ionic-native/native-geocoder": "^5.26.0",
        "@ionic-native/native-storage": "^5.23.0",
        "@ionic-native/onesignal": "^5.21.5",
        "@ionic-native/social-sharing": "^5.21.5",
        "@ionic-native/splash-screen": "^5.0.0",
        "@ionic-native/status-bar": "^5.21.5",
        "@ionic/angular": "^5.0.0",
        "@ionic/storage": "^2.2.0",
        "@ngx-translate/core": "^12.1.2",
        "@ngx-translate/http-loader": "^4.0.0",
        "cordova-plugin-add-swift-support": "^2.0.2",
        "cordova-plugin-advanced-http": "^2.4.1",
        "cordova-plugin-android-permissions": "^1.0.2",
        "cordova-plugin-appminimize": "^1.0.1",
        "cordova-plugin-apprate": "^1.5.0",
        "cordova-plugin-crop": "^0.4.0",
        "cordova-plugin-dialogs": "^2.0.2",
        "cordova-plugin-email-composer": "^0.9.2",
        "cordova-plugin-facebook4": "^6.4.0",
        "cordova-plugin-file": "^6.0.2",
        "cordova-plugin-file-transfer": "^1.7.1",
        "cordova-plugin-geolocation": "^4.0.2",
        "cordova-plugin-googleplus": "8.4.0",
        "cordova-plugin-inappbrowser": "^3.2.0",
        "cordova-plugin-nativegeocoder": "^3.4.1",
        "cordova-plugin-nativestorage": "^2.3.2",
        "cordova-plugin-request-location-accuracy": "^2.3.0",
        "cordova-plugin-statusbar": "^2.4.3",
        "cordova-plugin-telerik-imagepicker": "git+https://github.com/Telerik-Verified-Plugins/ImagePicker.git",
        "cordova-plugin-x-socialsharing": "^5.6.4",
        "cordova-sqlite-storage": "^5.0.0",
        "core-js": "^2.5.4",
        "es6-promise-plugin": "^4.2.2",
        "onesignal-cordova-plugin": "^2.8.3",
        "rxjs": "~6.5.1",
        "tslib": "^1.9.0",
        "zone.js": "~0.9.1"
      ,
      "devDependencies": 
        "@angular-devkit/build-angular": "~0.803.20",
        "@angular/cli": "~8.3.23",
        "@angular/compiler": "~8.2.14",
        "@angular/compiler-cli": "~8.2.14",
        "@angular/language-service": "~8.2.14",
        "@ionic/angular-toolkit": "^2.1.1",
        "@types/jasmine": "~3.3.8",
        "@types/jasminewd2": "~2.0.3",
        "@types/node": "~8.9.4",
        "codelyzer": "^5.0.0",
        "cordova-android": "^9.0.0",
        "cordova-plugin-device": "^2.0.2",
        "cordova-plugin-ionic-keyboard": "^2.2.0",
        "cordova-plugin-ionic-webview": "^4.1.3",
        "cordova-plugin-splashscreen": "^5.0.2",
        "cordova-plugin-whitelist": "^1.3.3",
        "jasmine-core": "~3.4.0",
        "jasmine-spec-reporter": "~4.2.1",
        "karma": "^4.4.1",
        "karma-chrome-launcher": "~2.2.0",
        "karma-coverage-istanbul-reporter": "~2.0.1",
        "karma-jasmine": "~2.0.1",
        "karma-jasmine-html-reporter": "^1.4.0",
        "protractor": "~5.4.0",
        "ts-node": "~7.0.0",
        "tslint": "~5.15.0",
        "typescript": "~3.4.3"
      ,
      "description": "An Ionic project",
      "cordova": 
        "plugins": 
          "cordova-plugin-email-composer": ,
          "cordova-plugin-x-socialsharing": ,
          "onesignal-cordova-plugin": ,
          "cordova-plugin-apprate": ,
          "cordova-plugin-appminimize": ,
          "cordova-plugin-whitelist": ,
          "cordova-plugin-device": ,
          "cordova-plugin-splashscreen": ,
          "cordova-plugin-ionic-webview": ,
          "cordova-plugin-ionic-keyboard": ,
          "cordova-plugin-advanced-http": ,
          "cordova-plugin-statusbar": ,
          "cordova-plugin-crop": ,
          "cordova-plugin-file-transfer": ,
          "cordova-plugin-telerik-imagepicker": 
            "PHOTO_LIBRARY_USAGE_DESCRIPTION": "This will allow you to add product images",
            "ANDROID_SUPPORT_V7_VERSION": "27.+"
          ,
          "cordova-plugin-nativestorage": ,
          "cordova-plugin-inappbrowser": ,
          "cordova-plugin-googleplus": 
            "REVERSED_CLIENT_ID": "com.googleusercontent.apps.806565206717-kmpd2cgn3prfe3e7gufh1uaq4r0sp98a",
            "PLAY_SERVICES_VERSION": "15.0.1"
          ,
          "cordova-plugin-facebook4": 
            "APP_ID": "691375698089216",
            "APP_NAME": "Quick Order",
            "FACEBOOK_HYBRID_APP_EVENTS": "false",
            "FACEBOOK_ANDROID_SDK_VERSION": "5.13.0"
          ,
          "cordova-sqlite-storage": ,
          "cordova-plugin-geolocation": ,
          "cordova-plugin-request-location-accuracy": 
            "PLAY_SERVICES_LOCATION_VERSION": "16.+"
          ,
          "cordova-plugin-nativegeocoder": 
            "LOCATION_WHEN_IN_USE_DESCRIPTION": "Use geocoder service"
          ,
          "cordova-plugin-android-permissions": 
        ,
        "platforms": [
          "android"
        ]
      
    

谁能帮我解决这个问题?

【问题讨论】:

您能否提供有关出现的任何错误的详细信息?您正在运行什么cordova/ionic 命令?当您说“最近更新后”时,您是指更新一些代码,还是更新版本? 【参考方案1】:

出现构建/编译错误时会出现持续的白屏。确保首先在本地(浏览器)服务以检查错误。然后通过 USB 连接您的设备并使用计算机的 chrome 检查工具 (chrome://inspect/#devices) 检查已安装版本的问题。如果file ///android_asset/www/index.html 问题无关,您可能会在这里找到它。如果您最近进行了更新,则可能是依赖问题或相关问题。

您也可以尝试ionic cordova run android 代替投放,以防投放时出现问题。您提到的网络错误可能与 CORS 有关,但我倾向于说可能不是。

如果您可以更新您的问题以包含您收到的错误以及命令,这将有助于缩小范围。

【讨论】:

【参考方案2】:

通常是因为在加载时,无法到达 index.html 文件。确保android平台内部存在www文件夹,并且您应该检查您的Android App中的FileSystem路径是否仍然相同并且任何插件已经覆盖了该路径。

另外可能应该卸载所有插件并开始一个尝试/错误过程一个一个安装,只是为了确保任何插件都不能正常工作。

【讨论】:

以上是关于Ionic 应用程序在浏览器和 ios 上运行,但不在 android 上,与服务器的连接不成功。 (文件///android_asset/www/index.html)在android上的主要内容,如果未能解决你的问题,请参考以下文章

在 IOS 上调试 Ionic 应用程序?

Ionic 4 Leaflet Map不适用于部署,但适用于离子服务--devapp

Ionic 5 - API 请求在浏览器上工作,而不是在模拟的 IOS 上

新标签 ionic starter 项目在 ios 模拟器和 ios 设备上简单崩溃的问题。但如果使用离子服务则有效

当 Ionic 3 应用程序在大于 9.3 的 ios 上运行时,Xcode 给出异常

IOS 10 或 11 上的 Ionic 3 标头高度