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

问题描述 投票:50回答:6

从Android 9 Pie开始,没有加密的请求永远不会起作用。默认情况下,系统会要求您默认使用TLS.You can read this feature here因此,如果您只通过HTTPS发出请求,那么您就是安全的。但是那些通过不同网站提出请求的应用程序呢,例如类似浏览器的应用程序。

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

java android kotlin android-9.0-pie android-network-security-config
6个回答
97
投票

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

android:usesCleartextTraffic="true"

但是,如果您想要为不同的链接提供更多配置,例如,允许http用于某些域而不是其他域,则必须提供networkSecurityConfig文件。

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

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




    </application>
</manifest>

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

<?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>

从那里你很高兴。现在,您的应用程序将请求所有类型的连接。有关此主题的更多信息,请访问read here


10
投票

针对此问题的AndroidReact-native用户的完全工作解决方案只需在AndroidManifest.xml文件中添加此android:usesCleartextTraffic="true",如下所示:

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>

4
投票

一个简单的方法是在你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>

我希望这能帮到您。


3
投票

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

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

2
投票

对于运行在调试中的React Native应用程序,将@Xenolion提到的xml block添加到位于react_native_config.xml<project>/android/app/src/debug/res/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>

0
投票

您可以检查是否通过HTTP修复发送clearText:https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6 要么 在Apache HTTP客户端弃用案例中(来自Google):在Android 6.0中,我们删除了对Apache HTTP客户端的支持。从Android 9开始,该库将从bootclasspath中删除,默认情况下不可用于应用程序。要继续使用Apache HTTP客户端,针对Android 9及更高版本的应用程序可以将以下内容添加到其AndroidManifest.xml中:

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

© www.soinside.com 2019 - 2024. All rights reserved.