无法加载资源:net :: ERR_CLEARTEXT_NOT_PERMITTED

时间:2019-11-30 13:49:13

标签: android cordova http ionic4

我遇到了这个问题link,并且进行了后续更改,但仍然无法解决问题。仍然给我同样的错误。 有人可以帮我吗。提前致谢 这是我的config.xml中的代码

 <widget id="com.farm.fork" version="0.0.1" 
    xmlns="http://www.w3.org/ns/widgets"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cdv="http://cordova.apache.org/ns/1.0">


<platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" >
            <application android:usesCleartextTraffic="true" />
            <application android:networkSecurityConfig="@xml/network_security_config" />
        </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

AndoidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.farm.fork" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:networkSecurityConfig="@xml/network_security_config" android:supportsRtl="true" android:usesCleartextTraffic="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:enabled="true" android:exported="false" android:name="com.google.android.gms.measurement.AppMeasurementService" />
        <service android:name="org.apache.cordova.firebase.FirebasePluginMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <receiver android:name="org.apache.cordova.firebase.OnNotificationOpenReceiver" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/accent" />
        <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id" />
        <meta-data android:name="firebase_analytics_collection_enabled" android:value="true" />
        <meta-data android:name="firebase_performance_collection_enabled" android:value="true" />
        <meta-data android:name="firebase_crashlytics_collection_enabled" android:value="true" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-feature android:name="android.hardware.location.gps" />
</manifest>

5 个答案:

答案 0 :(得分:5)

打开AndroidManifest.xml并将以下行添加到<application>标签:

android:usesCleartextTraffic="true"

请参考下图:

enter image description here

答案 1 :(得分:0)

按照@Swayangjit的建议,我在network_security_config.xml中添加了基本配置标签。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

  <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
  </base-config>

    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.43.147:3000</domain> <!--Ip address-->
        <domain includeSubdomains="true">192.168.43.147:3000/products</domain>
        <domain includeSubdomains="true">10.0.2.2 </domain>

    </domain-config>
</network-security-config>

在那之后,我得到了连接被拒绝的错误,因为我试图通过移动设备连接到本地主机。相反,您必须使用您的IP地址。 例如:http://localhost:3000在android设备上不起作用,而您必须像这样使用本地网络的IP地址

http://IP-addrees:3000。 注意:您可以通过ipconfig命令

查找IP地址。

答案 2 :(得分:0)

经过几天的努力,这对我有用,我希望这对您也有用。 将其添加到代码顶部的CONFIG.XML中。

<access origin="*" />
<allow-navigation href="*" />

和这个,在android平台下。

<edit-config file="app/src/main/AndroidManifest.xml" 
   mode="merge" target="/manifest/application" 
   xmlns:android="http://schemas.android.com/apk/res/android">
     <application android:usesCleartextTraffic="true" />
     <application android:networkSecurityConfig="@xml/network_security_config" />
 </edit-config>
 <resource-file src="resources/android/xml/network_security_config.xml" 
 target="app/src/main/res/xml/network_security_config.xml" />

将以下代码添加到此文件“ resources / android / xml / network_security_config.xml”。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">YOUR DOMAIN HERE/IP</domain>
    </domain-config>
</network-security-config>

enter image description here

enter image description here

答案 3 :(得分:0)

This work for me!!

路径:.. \ resources \ android \ xml \ network_security_config.xml

**<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
        <domain includeSubdomains="true">Ip address</domain>
    </domain-config>
</network-security-config>**

答案 4 :(得分:-1)

尝试更改您的network_security_config.xml

来自:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
       <domain includeSubdomains="true">localhost</domain>
    </domain-config>
</network-security-config>

至:

<?xml version="1.0" encoding="utf-8"?>    
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">localhost</domain>
    </base-config>
</network-security-config>

请注意将标记 domain-config 更改为 基本配置