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

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


实现这一点的简单方法是使用这个属性到你的AndroidManifest.xml,你允许所有的http请求:

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

但如果你想要更多的配置不同的链接,例如,允许http的一些域,但不允许其他域,你必须提供res/xml/networkSecurityConfig.xml文件。

要在Android 9 Pie中做到这一点,你必须在Manifest应用程序标签中设置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>

然后你就可以继续了。现在你的应用程序将对所有类型的连接发出请求。有关此主题的更多信息,请阅读此处。


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

完全工作的解决方案,无论是Android或react原生用户面对这个问题,只需添加这个 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>

You may check if you are sending clearText through HTTP Fix : https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6 OR In the Case of Apache HTTP client deprecation (From Google ) : With Android 6.0, we removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default. To continue using the Apache HTTP client, apps that target Android 9 and above can add the following to their AndroidManifest.xml:

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


只需在AndroidManifest.xml文件的应用程序标签中设置usesCleartextTraffic标志。 不需要为Android创建配置文件。

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

一个简单的方法是设置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”。


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

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

所以我把标签“domain-config”改为“base-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>