为什么带有明文的HTTP流量错误ERR_CLEARTEXT_NOT_PERMITTED持续存在? [英] Why is the error ERR_CLEARTEXT_NOT_PERMITTED with cleartext for the HTTP traffic persistent?
问题描述
我正在使用Ionic-framework开发一个新的应用程序,并且正在使用HttpClient模块进行API请求。
I'm developing a new app using the Ionic-framework and I'm using the HttpClient module for API requests.
问题是我已阅读并尝试将解决方案应用于以下位置:
The problem is that I've read and tried to apply the solutions on:
- https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9- pie-2f4e9e2235e6
- https://forum.ionicframework.com/t/livereload-err-cleartext-not-permitted/163487
- Android 8:不允许使用明文HTTP流量
- 为什么我看到net :: ERR_CLE升级到Cordova Android 8后出现ARTEXT_NOT_PERMITTED错误吗?
- 如何快速解决'net :: ERR_CLEARTEXT_NOT_PERMITTED'
- Android Pie:即使在useClearTextTraffic =的情况下,WebView也会在某些站点上显示纯HTTP错误; true
- WebView显示ERR_CLEARTEXT_NOT_PERMITTED,尽管网站是HTTPS
- https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
- https://forum.ionicframework.com/t/livereload-err-cleartext-not-permitted/163487
- Android 8: Cleartext HTTP traffic not permitted
- Why am I seeing net::ERR_CLEARTEXT_NOT_PERMITTED errors after upgrading to Cordova Android 8?
- How to fix 'net::ERR_CLEARTEXT_NOT_PERMITTED' in flutter
- Android Pie: WebView showing error for plain HTTP on some sites, even with usesClearTextTraffic="true"
- WebView showing ERR_CLEARTEXT_NOT_PERMITTED although site is HTTPS
应用程序对API进行查询时不断抛出此错误。
But my app keeps throwing this error when it does a query to the API.
以下是我文件的详细信息:
Here are the details of my files:
在 / , config.xml :
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" 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" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="@xml/network_security_config"/>
<application android:usesCleartextTraffic="true" />
</edit-config>
<resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
...
</widget>
位于 / resources / android / 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>
使用安全加密的协议HTTPS是无效的。该API仅允许使用HTTP。
It is not valid to use the securely encrypted protocol HTTPS. The API only admits HTTP.
推荐答案
更新2019年12月ionic-4.7.1
<manifest xmlns:tools="http://schemas.android.com/tools">
<application android:usesCleartextTraffic="true" tools:targetApi="28">
请在android manifest.xml文件中添加以上内容
Please add above content in android manifest .xml file
ionic的先前版本
-
请确保您的<$ Ionic Project中的c $ c> config.xml :
<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:networkSecurityConfig="@xml/network_security_config" />
<application android:usesCleartextTraffic="true" />
</edit-config>
运行ionic cordova构建android。它将在Platforms下创建Android文件夹
Run ionic Cordova build android. It creates Android folder under Platforms
打开Android Studio并打开存在于我们的项目
project-platforms-android中的Android文件夹。留下几分钟,以便它构建gradle
Open Android Studio and open the Android folder present in our project project-platforms-android. Leave it for few minutes so that it builds the gradle
在完成渐变构建
之后,我们得到了一些在 manifest.xml
中包含 minSdVersion
的错误。
现在我们要做的就是从 manifest.xml <中删除
< uses-sdk android:minSdkVersion = 19 />
/ code>。
After gradle build
is finished we get some errors for including minSdVersion
in manifest.xml
.
Now what we do is just remove <uses-sdk android:minSdkVersion="19" />
from manifest.xml
.
请确保将其从两个位置删除:
Make sure its removed from both the locations:
- app→清单→
AndroidManifest.xml
。 - CordovaLib→清单→
AndroidManifest.xml
。
- app → manifests →
AndroidManifest.xml
. - CordovaLib → manifests →
AndroidManifest.xml
.
现在尝试再次构建gradle,现在它可以成功构建
Now try to build the gradle again and now it builds successfully
确保在App&rarr中的Application标签中具有以下内容:清单→ Androidmanifest.xml
:
Make sure you have the following in Application tag in App → manifest → Androidmanifest.xml
:
<application
android:networkSecurityConfig="@xml/network_security_config" android:usesCleartextTraffic="true" >
打开 network_security_config
(app&rarr ; res→ xml→ network_security_config.xml
)。
Open network_security_config
(app → res → xml → network_security_config.xml
).
添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">xxx.yyyy.com</domain>
</domain-config>
</network-security-config>
此处 xxx。 yyyy.com
是HTTP API的链接。确保您在URL之前不包含任何Http。
Here xxx.yyyy.com
is the link of your HTTP API. Make sure you don't include any Http before the URL.
注意:现在使用Android Studio构建应用(构建-构建捆绑包的/ APK-构建APK)现在您可以使用该应用了,它在Android Pie中也可以正常运行。如果您尝试使用ionic Cordova build android来构建应用程序,它将覆盖所有这些设置,因此请确保使用Android Studio来构建Project。
Note: Now build the app using Android Studio (Build -- Build Bundle's/APK -- Build APK) and now you can use that App and it works fine in Android Pie. If you try to build app using ionic Cordova build android it overrides all these settings so make sure you use Android Studio to build the Project.
如果您有任何旧版本的应用程序已安装,请卸载它们并尝试一下,否则您将遇到一些错误:
If you have any older versions of app installed, Uninstall them and give a try or else you will be left with some error:
未安装应用程序
App not Installed
这篇关于为什么带有明文的HTTP流量错误ERR_CLEARTEXT_NOT_PERMITTED持续存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!