为什么 HTTP 流量的明文错误 ERR_CLEARTEXT_NOT_PERMITTED 持续存在? [英] Why is the error ERR_CLEARTEXT_NOT_PERMITTED with cleartext for the HTTP traffic persistent?

查看:24
本文介绍了为什么 HTTP 流量的明文错误 ERR_CLEARTEXT_NOT_PERMITTED 持续存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Ionic 框架开发一个新的应用程序,我正在使用 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:

  1. https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
  2. https://forum.ionicframework.com/t/livereload-err-cleartext-not-permitted/163487
  3. Android 8:不允许明文 HTTP 流量
  4. 为什么我升级到 Cordova Android 8 后看到 net::ERR_CLEARTEXT_NOT_PERMITTED 错误?
  5. 如何修复颤振中的net::ERR_CLEARTEXT_NOT_PERMITTED"
  6. Android饼图:WebView 在某些站点上显示纯 HTTP 错误,即使使用 usesClearTextTraffic="true"
  7. WebView尽管网站是 HTTPS,但显示 ERR_CLEARTEXT_NOT_PERMITTED

但是我的应用程序在对 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

离子的以前版本

  1. 确保在 Ionic 项目的 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 build android.它会在 Platforms 下创建 Android 文件夹

  • Run ionic Cordova build android. It creates Android folder under Platforms

    打开 Android Studio 并打开我们项目中的 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

    gradle build 完成后,我们会因为在manifest.xml 中包含minSdVersion 而出现一些错误.现在我们要做的只是从 manifest.xml 中删除 <uses-sdk android:minSdkVersion="19"/>.

    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:

    1. 应用程序 →清单 →AndroidManifest.xml.
    2. CordovaLib →清单 →AndroidManifest.xml.

    现在再次尝试构建gradle,现在构建成功

    Now try to build the gradle again and now it builds successfully

    确保您在应用程序中的应用程序标签中具有以下内容 →清单 →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 → 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 (Build -- Build Bundle's/APK -- Build APK) 构建应用程序,现在您可以使用该应用程序并且它在 Android Pie 中运行良好.如果您尝试使用 ionic Cordova build android 构建应用程序,它会覆盖所有这些设置,因此请确保您使用 Android Studio 构建项目.

    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:

    应用未安装

    这篇关于为什么 HTTP 流量的明文错误 ERR_CLEARTEXT_NOT_PERMITTED 持续存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆