Internet权限在Oreo和Pie中不起作用 [英] Internet permission not working in oreo and pie

查看:126
本文介绍了Internet权限在Oreo和Pie中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.我构建了一个简单的应用程序,该应用程序将一些数据发送到服务器并接收响应.

I'm new here. I built a simple app that sends some data to server and as well receives the response.

该应用在android N以下的设备上均可正常运行.但在O和P上则无法正常运行.似乎问题出在互联网许可范围内.我已经检查过,该应用程序在o和p上运行时未向服务器发送任何数据.如果需要我的互联网访问权限,请告诉我.

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.

我已在清单中添加了它

<uses-permission android:name="android.permission.INTERNET" />

任何帮助将不胜感激.

推荐答案

可能是因为您使用的是http.从Android O开始,您需要使用https而不是http,否则会出现错误Cleartext HTTP traffic to * not permitted.因此,您需要创建一个配置以允许这样做.您可以将其引用到退出明文流量

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

文档详细信息:

注意:本节中的指导仅适用于定位到 Android 8.1(API等级27)或更低版本.从Android 9(API 级别28),默认情况下会禁用明文支持.

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.

打算仅使用安全连接到目的地的应用程序 连接可以选择不支持明文(使用未加密的 HTTP协议而不是HTTPS).这个选项 有助于防止由于URL更改而导致应用程序意外退缩 由外部资源(例如后端服务器)提供.看 NetworkSecurityPolicy.isCleartextTrafficPermitted()有关更多详细信息.

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.

例如,某个应用可能想要确保所有与 secure.example.com总是通过HTTPS完成以保护敏感 来自敌对网络的流量.

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>

.

您还可以在AndroidManifest.xml中将android:usesCleartextTraffic="true"用于开发模式,但不应在发布模式下使用它.有关它的更多详细信息,请参见 Android开发者博客,此处摘录:

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:

阻止生产中的明文流量

Block cleartext traffic in production

为了保护您的应用程序的安装基础免受回归到 明文流量,声明android:usesCleartextTraffic ="false" 属性位于应用程序的应用程序元素中 AndroidManifest.xml.这声明该应用程序不应该使用 明文网络流量,使平台网络堆栈 Android Marshmallow阻止应用程序中的明文流量.例如, 如果您的应用程序意外尝试通过明文登录用户 HTTP请求,该请求将被阻止,并且用户的身份和 密码不会泄漏到网络.

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.

您不必将应用程序的minSdkVersion或targetSdkVersion设置为 23(Android Marshmallow)以使用android:usesCleartextTraffic.在老 在平台上,该属性将被忽略,因此无效.

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.

请注意,WebView尚不支持此功能.

Please note that WebView does not yet honor this feature.

在某些情况下,明文流量仍可能离开或 输入应用程序.例如,Socket API会忽略明文策略 因为它不知道它发送或接收的数据是否可以 被归类为明文. Android平台HTTP堆栈,另一方面 尊重政策,因为他们知道流量是否为明文.

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也是为了遵守该政策而创建的.当您的应用程序 声明它不使用纯文本流量,仅使用HTTPS广告 应该投放到该应用.

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.

鼓励第三方网络,广告和分析库添加 对此政策的支持.他们可以查询明文流量策略 通过NetworkSecurityPolicy类.

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.

这篇关于Internet权限在Oreo和Pie中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆