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

Xen*_*ion 100 java android kotlin android-network-security-config android-9.0-pie

从Android 9 Pie开始,没有加密的请求永远不会起作用.默认情况下,系统会指望您默认使用TLS.您可以在此处阅读此功能因此,如果您只通过HTTPS发出请求,那么您就是安全的.但是那些通过不同网站提出请求的应用程序呢,例如类似浏览器的应用程序.

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

Xen*_*ion 168

要在Android 9 Pie中执行此操作,您必须AndroidManifest.xml在Manifest http标记中设置如下:

android:usesCleartextTraffic="true"
Run Code Online (Sandbox Code Playgroud)

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

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




    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

从那里你很高兴.现在,您的应用程序将请求所有类型的连接.有关其他信息在这里阅读.

  • @Xenolion 在我进行这些更改(使用 React Native 应用程序)后,它不再构建。“清单合并不成功”。“AndroidManifest.xml:7:7-67 中的属性 application@networkSecurityConfig value=(@xml/react_native_config) 也出现在 AndroidManifest.xml:7:7-67 value=(@xml/network_security_config) 中。有什么想法吗? (2认同)
  • 我在哪里可以找到本机项目中的xml文件夹 (2认同)
  • 浪费大量时间弄清楚这实际上是HTTP问题。通常它在HTTP响应上显示错误 (2认同)

Har*_*wal 24

对于这两个或用户都面临此问题的完全有效的解决方案,只需将其添加 到AndroidManifest.xml文件中,如下所示:AndroidReact-nativeandroid:usesCleartextTraffic="true"

android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
    android:name="org.apache.http.legacy"
    android:required="false" />
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

  • 如果你收到 `tools:ignore` 错误,请确保在你的 `application` 中添加 `xmlns:tools="http://schemas.android.com/tools"`。像这样`&lt;application xmlns:tools="http://schemas.android.com/tools" ...` (6认同)
  • 哇,感谢它在我的应用程序中运行良好,在它显示 i/o 失败问题之前解决了 (2认同)
  • 我知道这是一个android问题,但可能对react-native开发人员有帮助,ios解决方案是将`NSAppTransportSecurity`添加到info.plist。/sf/ask/2689329891/ (2认同)

Ars*_* KV 14

简单的方法

添加usesCleartextTrafficAndroidManifest.xml

<application
...
android:usesCleartextTraffic="true"
...>
Run Code Online (Sandbox Code Playgroud)

指示应用程序是否打算使用明文网络流量,例如明文 HTTP。面向 API级别 27或更低级别的应用程序的默认值为“true”。面向API级别 28或更高级别的应用默认为“false”。


Meh*_*nki 10

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

android:usesCleartextTraffic="true"
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您。


小智 5

对于React Native在调试中运行的应用程序,将xml block@Xenolion提及的内容添加到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>
Run Code Online (Sandbox Code Playgroud)


Asi*_*tel 5

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

 <application
   android:usesCleartextTraffic="true"
   .
   .
   .>
Run Code Online (Sandbox Code Playgroud)