如何在Android (9) Pie中允许所有网络连接类型HTTP和HTTPS?



从Android 9 Pie开始,没有加密的请求将永远无法正常工作。默认情况下,系统将期望您默认使用 TLS。您可以在此处阅读此功能,因此,如果您仅通过HTTPS发出请求,那么您是安全的。但是,通过不同网站发出请求的应用程序呢,例如类似浏览器的应用程序。

如何在 Android 9 Pie 中启用对所有类型的连接 HTTP 和 HTTPS 的请求?

实现这一点的简单方法是将此属性用于您的AndroidManifest.xml,其中您允许所有请求的所有http

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

但是,如果您想为不同的链接进行更多配置,例如,允许对某些域进行http,但不允许对其他域进行res/xml/networkSecurityConfig.xml则必须提供文件。

要在Android 9 Pie中执行此操作,您必须在清单application标签中设置networkSecurityConfig,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config">


</application>
</manifest>

然后在xml文件夹中,您现在必须创建一个名为network_security_config的文件,就像您在清单中命名它的方式一样,从那里文件的内容应该是这样的,以启用所有没有加密的请求:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>

从那里你很好去。现在,你的应用将请求所有类型的连接。有关此主题的其他信息,请阅读此处。

对于面临此问题的AndroidReact-native用户来说,完全有效的解决方案只需添加此android:usesCleartextTraffic="true"AndroidManifest 中.xml像这样的文件:

android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />

<application>之间..</application>这样的标记:

<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"/>
</application>

一个简单的方法设置android:usesCleartextTraffic="true"AndroidManifest.xml

android:usesCleartextTraffic="true"

您的AndroidManifest.xml看起来像

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.dww.drmanar">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme"
tools:targetApi="m">
<activity
android:name=".activity.SplashActivity"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

我希望这对你有帮助。

简单的方法

usesCleartextTraffic添加到AndroidManifest.xml

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

指示应用是否打算使用明文网络流量,如明文 HTTP。面向 API级别 27或更低版本的应用的默认值为"true"。以 API级别 28或更高版本为目标平台的应用默认为"false"。

对于在调试中运行时React Native应用程序,请将@Xenolion提到的xml block添加到位于<project>/android/app/src/debug/res/xmlreact_native_config.xml

类似于以下代码片段:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>
</domain-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>

只需在AndroidManifest.xml文件的应用程序标记中设置usesCleartextTraffic标志即可。 无需为安卓创建配置文件。

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

我遇到了同样的问题,我注意到我的安全配置有不同的标签,就像@Xenolion答案所说的那样

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

所以我将标签"域配置"更改为"基本配置"并工作,如下所示:

<network-security-config>
<base-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</base-config>
</network-security-config>

这对我有用,

将此 XML 文件添加到:andriod/app/src/main/res/xml/network_security_config.xml

network_security_config.xml

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">your_domain1</domain>
</domain-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">your_domain2</domain>
</domain-config>
</network-security-config>

然后将此代码添加到AndroidMenifest.xml

<application
...
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config"
...
>
<!-- for http support-->
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
...
</application>

将 usesCleartextTraffic 添加到 AndroidManifest.xml

例如

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

<application
android: allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="AndroidLearning"
android: supportsRtl="true"
android:UsesCleartextTraffic="true" <-----
android: theme="@style/Theme.AndroidLearning"
tools:targetApi="31">
<activity
android:name=" MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android. intent.action.MAIN" />
‹catedorv android:name="android.intent.catedorv.LAUNCHFR" />

您可以检查是否通过HTTP发送明文 修复 : https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
OR
在Apache HTTP客户端弃用的情况下(来自谷歌(: 在 Android 6.0 中,我们删除了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库将从引导类路径中移除,默认情况下对应用不可用。 要继续使用 Apache HTTP 客户端,面向 Android 9 及更高版本的应用可以将以下内容添加到其 AndroidManifest.xml:

源 https://developer.android.com/about/versions/pie/android-9.0-changes-28

最新更新