Java 如何在 Android (9) Pie 中允许所有网络连接类型 HTTP 和 HTTPS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51902629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to allow all Network connection types HTTP and HTTPS in Android (9) Pie?
提问by Xenolion
From Android 9 Pie now, requests without encryption will never work. And by default, the System will expect you to use TLS by default.You can read this feature hereSo if you only make requests via HTTPS you are safe. But what about apps that make requests through different sites, for instance, browser-like apps.
从现在的 Android 9 Pie 开始,没有加密的请求将永远无法工作。默认情况下,系统会期望您默认使用 TLS。您可以在此处阅读此功能因此,如果您仅通过 HTTPS 发出请求,那么您是安全的。但是通过不同站点发出请求的应用程序呢,例如类似浏览器的应用程序。
How can I enable requests to all types of connections HTTP and HTTPS in Android 9 Pie?
如何在 Android 9 Pie 中启用对所有类型连接 HTTP 和 HTTPS 的请求?
采纳答案by Xenolion
The easy way to implement this is to use this attribute to your AndroidManifest.xml
where you allow all http
for all requests:
实现这一点的简单方法是将此属性用于您AndroidManifest.xml
允许http
所有请求的位置:
<application android:usesCleartextTraffic="true">
</application>
But in case you want some more configurationsfor different links for instance, allowing http
for some domains but not other domains you must provide res/xml/networkSecurityConfig.xml
file.
但是,如果您想要为不同的链接进行更多配置,例如允许http
某些域但不允许其他域,您必须提供res/xml/networkSecurityConfig.xml
文件。
To do this in Android 9 Pie you will have to set a networkSecurityConfig
in your Manifest application
tag like this:
要在 Android 9 Pie 中执行此操作,您必须像这样networkSecurityConfig
在 Manifestapplication
标签中设置一个:
<?xml version="1.0" encoding="utf-8"?>
<manifest ... >
<application android:networkSecurityConfig="@xml/network_security_config">
</application>
</manifest>
Then in your xml
folder you now have to create a file named network_security_config
just like the way you have named it in the Manifest and from there the content of your file should be like this to enable all requests without encryptions:
然后在您的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>
From there you are good to go. Now your app will make requests for all types of connections. For additional information on this topic read here.
从那里你很高兴去。现在您的应用程序将对所有类型的连接发出请求。有关此主题的更多信息,请阅读此处。
回答by Kaviyarasu Duraisamy
For React Native
applications while running in debug add the xml block
mentioned by @Xenolion to react_native_config.xml
located in <project>/android/app/src/debug/res/xml
对于React Native
在调试中运行的应用程序,将xml block
@Xenolion 提到的添加到react_native_config.xml
位于<project>/android/app/src/debug/res/xml
Similar to the following snippet:
类似于以下代码段:
<?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>
回答by Harshit Agrawal
The FULLY WORKING SOLUTIONfor both Android
or React-native
users facing this issue just add this
android:usesCleartextTraffic="true"
in AndroidManifest.xmlfile like this:
在岗工作液两种Android
或React-native
面临这个问题,用户只需添加这
android:usesCleartextTraffic="true"
在AndroidManifest.xml中像这样的文件:
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
in between <application>
.. </application>
tag like this:
在<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>
回答by user11091094
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:
您可以检查您是否通过 HTTP Fix 发送明文:https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
或
在 Apache HTTP 客户端的情况下弃用(来自 Google):在 Android 6.0 中,我们取消了对 Apache HTTP 客户端的支持。从 Android 9 开始,该库从引导类路径中删除,默认情况下不可用于应用程序。要继续使用 Apache HTTP 客户端,面向 Android 9 及更高版本的应用程序可以将以下内容添加到其 AndroidManifest.xml 中:
Source https://developer.android.com/about/versions/pie/android-9.0-changes-28
来源 https://developer.android.com/about/versions/pie/android-9.0-changes-28
回答by Asif Patel
Just set usesCleartextTraffic
flag in the application tag of AndroidManifest.xml
file.
No need to create config file for Android.
只需usesCleartextTraffic
在AndroidManifest.xml
文件的应用程序标签中设置标志。无需为 Android 创建配置文件。
<application
android:usesCleartextTraffic="true"
.
.
.>
回答by Mehul Solanki
A simple way is set android:usesCleartextTraffic="true"
on you AndroidManifest.xml
一个简单的方法android:usesCleartextTraffic="true"
在你身上AndroidManifest.xml
android:usesCleartextTraffic="true"
Your AndroidManifest.xml
look like
你的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>
I hope this will help you.
我希望这能帮到您。
回答by Arshid KV
Easy Way
简单的方法
Add usesCleartextTraffic
to AndroidManifest.xml
添加usesCleartextTraffic
到AndroidManifest.xml
<application
...
android:usesCleartextTraffic="true"
...>
Indicates whether the app intends to use cleartext network traffic, such as cleartext HTTP. The default value for apps that target API level 27or lower is "true". Apps that target API level 28or higher default to "false".
指示应用程序是否打算使用明文网络流量,例如明文 HTTP。面向 API级别 27或更低级别的应用程序的默认值为“true”。面向API级别 28或更高级别的应用默认为“false”。
回答by Sandy Veliz
i got the same problem and i notice that my security config has diferent TAGS like the @Xenolion answer says
我遇到了同样的问题,我注意到我的安全配置有不同的标签,比如@Xenolion 的回答说
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
so i change the TAGS "domain-config" for "base-config" and works, like this:
所以我将标签“域配置”更改为“基本配置”并工作,如下所示:
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</base-config>
</network-security-config>