Java 互联网权限在奥利奥和派中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55074899/
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
Internet permission not working in oreo and pie
提问by Jameel
I'm new here. I built a simple app that sends some data to server and as well receives the response.
我是新来的。我构建了一个简单的应用程序,将一些数据发送到服务器并接收响应。
The app is working fine on devices up to android N. But not working on O and P. Seems like the issue is with the internet permission. I have checked and the app is not sending any data to server while it is running on o and p. Please let me know if i need to seek any specific permission for internet access.
该应用程序在高达 android N 的设备上运行良好。但不适用于 O 和 P。似乎问题出在互联网许可上。我已经检查过,该应用程序在 o 和 p 上运行时没有向服务器发送任何数据。如果我需要寻求任何特定的互联网访问许可,请告诉我。
I have added this in manifest
我在清单中添加了这个
<uses-permission android:name="android.permission.INTERNET" />
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by ??? ???? ????
It probably because you're using http
. Starting from Android O, you need to use https
instead of http
or you'll have an error Cleartext HTTP traffic to * not permitted
. So, you need to create a configuration to allow this. You can refer it to Opt out of cleartext traffic
这可能是因为您正在使用http
. 从 Android O 开始,需要使用https
而不是,http
否则会报错Cleartext HTTP traffic to * not permitted
。因此,您需要创建一个配置来允许这样做。您可以参考选择退出明文流量
Details of documentation:
文件详情:
Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default.
Applications intending to connect to destinations using only secure connections can opt-out of supporting cleartext (using the unencrypted HTTP protocol instead of HTTPS) to those destinations. This option helps prevent accidental regressions in apps due to changes in URLs provided by external sources such as backend servers. See
NetworkSecurityPolicy.isCleartextTrafficPermitted()
for more details.For example, an app may want to ensure that all connections to
secure.example.com
are always done over HTTPS to protect sensitive traffic from hostile networks.
res/xml/network_security_config.xml
:<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="false"> <domain includeSubdomains="true">secure.example.com</domain> </domain-config> </network-security-config>
注意:本节中的指南仅适用于面向 Android 8.1(API 级别 27)或更低版本的应用。从 Android 9(API 级别 28)开始,默认情况下禁用明文支持。
打算仅使用安全连接连接到目标的应用程序可以选择不支持这些目标的明文(使用未加密的 HTTP 协议而不是 HTTPS)。此选项有助于防止由于后端服务器等外部来源提供的 URL 发生变化而导致应用程序意外回归。有关
NetworkSecurityPolicy.isCleartextTrafficPermitted()
更多详细信息,请参阅 。例如,应用程序可能希望确保所有连接
secure.example.com
始终通过 HTTPS 完成,以保护来自恶意网络的敏感流量。
res/xml/network_security_config.xml
:<?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="false"> <domain includeSubdomains="true">secure.example.com</domain> </domain-config> </network-security-config>
.
.
You can also use android:usesCleartextTraffic="true"
in your AndroidManifest.xml
for your development mode but you should not use it in release mode. More details about it in Android Developer Blog, here the excerpts:
您也可以android:usesCleartextTraffic="true"
在您AndroidManifest.xml
的开发模式中使用它,但不应在发布模式下使用它。有关它的更多详细信息,请参阅Android 开发者博客,这里是摘录:
Block cleartext traffic in production
To protect the installed base of your app against regressions to cleartext traffic, declare android:usesCleartextTraffic=”false” attribute on the application element in your app's AndroidManifest.xml. This declares that the app is not supposed to use cleartext network traffic and makes the platform network stacks of Android Marshmallow block cleartext traffic in the app. For example, if your app accidentally attempts to sign in the user via a cleartext HTTP request, the request will be blocked and the user's identity and password will not leak to the network.
You don't have to set minSdkVersion or targetSdkVersion of your app to 23 (Android Marshmallow) to use android:usesCleartextTraffic. On older platforms, this attribute is simply ignored and thus has no effect.
Please note that WebView does not yet honor this feature.
And under certain circumstances cleartext traffic may still leave or enter the app. For example, Socket API ignores the cleartext policy because it does not know whether the data it transmits or receives can be classified as cleartext. Android platform HTTP stacks, on the other hand, honor the policy because they know whether traffic is cleartext.
Google AdMob is also built to honor this policy. When your app declares that it does not use cleartext traffic, only HTTPS-only ads should be served to the app.
Third-party network, ad, and analytics libraries are encouraged to add support for this policy. They can query the cleartext traffic policy via the NetworkSecurityPolicy class.
阻止生产中的明文流量
为了保护您的应用程序的安装基础免受明文流量的回归,请在您的应用程序的 AndroidManifest.xml 中的应用程序元素上声明 android:usesCleartextTraffic=”false” 属性。这声明应用程序不应使用明文网络流量,并使 Android Marshmallow 的平台网络堆栈阻止应用程序中的明文流量。例如,如果您的应用意外尝试通过明文 HTTP 请求登录用户,则该请求将被阻止,并且用户的身份和密码不会泄露到网络。
您不必将应用的 minSdkVersion 或 targetSdkVersion 设置为 23 (Android Marshmallow) 即可使用 android:usesCleartextTraffic。在较旧的平台上,此属性被简单地忽略,因此无效。
请注意,WebView 尚不支持此功能。
在某些情况下,明文流量仍可能离开或进入应用程序。例如,Socket API 忽略了明文策略,因为它不知道它传输或接收的数据是否可以归类为明文。另一方面,Android 平台 HTTP 堆栈遵守该策略,因为它们知道流量是否为明文。
Google AdMob 也是为了遵守此政策而构建的。当您的应用声明它不使用明文流量时,应仅向应用投放 HTTPS 广告。
鼓励第三方网络、广告和分析库添加对此政策的支持。他们可以通过 NetworkSecurityPolicy 类查询明文流量策略。
回答by RAHUL MAURYA
If your URL start's with http
then you have to use it is used in Android Pie i.e API level 28
如果您的 URL 以开头,http
那么您必须使用它在 Android Pie 中使用,即 API 级别 28
android:usesCleartextTraffic="true"
in your manifest inside application tag as an attribute
在应用程序标签内的清单中作为属性
回答by Umar Farooq
回答by Richard Kamere
URL starting with http://you need to add the following attribute in your manifest file inside application tag.
以http://开头的 URL您需要在应用程序标签内的清单文件中添加以下属性。
android:usesCleartextTraffic="true"