首页 > 解决方案 > Angular NativeScript PlayGround Android 权限:android:usesCleartextTraffic="true"

问题描述

我在 Angular 方面进行了很多开发,并决定尝试使用 NativeScript 开发应用程序。

tns preview使用 OxygenOS 10.3.1 在我的 OnePlus 6t 上使用 Playground 应用程序扫描应用程序的 QR 码。手机连接到同一个wifi。我还有一个 nodejs 服务器,我想从 localhost:3000 上运行获取一些数据。

但是,当我尝试在手机上的应用程序中发出请求时,会出现此错误:

Error: java.io.IOException: Cleartext HTTP traffic to 192.168.1.4 not permitted\n    at onRequestCom...

因此,经过大量阅读了解我应该添加到我的 App_Resources/Android/src/main/AndroidManifest xml 文件中:

`

<application   
.
.
android:usesCleartextTraffic="true"
.
.
>

`

在此之后,我重新启动了应用程序,因为它没有直接将更新推送到手机上的应用程序(PlayGround)。但是没有任何改变,发生了同样的错误。

所以我尝试添加一个 network_security_config.xml 并写了这个:`

<?xml version="1.0" encoding="utf-8" ?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain> <!-- Debug port -->
        <domain includeSubdomains="true">xamarin.com</domain>
        <domain includeSubdomains="true">192.162.1.4:3000</domain> <!--My localhost domain on pc-->
    </domain-config>
</network-security-config>

`

然后在我的清单中:`

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="__PACKAGE__" android:versionCode="10000" android:versionName="1.0">

    <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:networkSecurityConfig="@xml/network_security_config" android:name="com.tns.NativeScriptApplication" android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" >

        <activity android:name="com.tns.NativeScriptActivity" android:label="@string/title_activity_kimera" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout|locale|uiMode" android:theme="@style/LaunchScreenTheme">

            <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.tns.ErrorReportActivity" />
    </application>
</manifest>

`

发生同样的错误。就像更改没有被推送到应用程序tns platform remove android然后tns platform add android,仍然没有工作,再次出现同样的错误。

感觉好像没有更新,但我不确定。帮帮我:D

我试过Android 8:不允许明文 HTTP 流量

我的 package.json:`

   {
      "nativescript": {
        "id": "org.nativescript.frontendapp",
        "tns-ios": {
          "version": "6.4.0"
        },
        "tns-android": {
          "version": "6.4.1"
        }
      },
      "description": "NativeScript Application",
      "license": "SEE LICENSE IN <your-license-filename>",
      "repository": "<fill-your-repository-here>",
      "dependencies": {
        "@angular/animations": "~8.2.0",
        "@angular/common": "~8.2.0",
        "@angular/compiler": "~8.2.0",
        "@angular/core": "~8.2.0",
        "@angular/forms": "~8.2.0",
        "@angular/platform-browser": "~8.2.0",
        "@angular/platform-browser-dynamic": "~8.2.0",
        "@angular/router": "~8.2.0",
        "@nativescript/theme": "~2.3.0",
        "nativescript-angular": "~8.21.0",
        "nativescript-permissions": "^1.3.8",
        "reflect-metadata": "~0.1.12",
        "rxjs": "^6.4.0",
        "tns-core-modules": "~6.4.0",
        "zone.js": "~0.9.1"
      },
      "devDependencies": {
        "@angular/compiler-cli": "~8.2.0",
        "@ngtools/webpack": "~8.2.0",
        "nativescript-dev-webpack": "~1.5.0",
        "typescript": "~3.5.3"
      },
      "gitHead": "d8bdf0799a219051f28f8abd7c258d2bf2fce6e1",
      "readme": "NativeScript Application"
    }

`

标签: nativescriptandroid-manifestandroid-permissionsnativescript-angularcleartext

解决方案


推荐阅读