简体   繁体   English

互联网权限在奥利奥和派中不起作用

[英]Internet permission not working in oreo and pie

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.该应用程序在高达 android N 的设备上运行良好。但不适用于 O 和 P。似乎问题出在互联网许可上。 I have checked and the app is not sending any data to server while it is running on o and p.我已经检查过,该应用程序在 o 和 p 上运行时没有向服务器发送任何数据。 Please let me know if i need to seek any specific permission for internet access.如果我需要寻求任何特定的互联网访问许可,请告诉我。

I have added this in manifest我在清单中添加了这个

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

Any help would be appreciated.任何帮助,将不胜感激。

If your URL start's with http then you have to use it is used in Android Pie ie API level 28 如果您的URL以http开头,那么您必须使用它在Android Pie即API级别28中使用

android:usesCleartextTraffic="true"

in your manifest inside application tag as an attribute 在应用程序标记内的清单中作为属性

It probably because you're using http . 这可能是因为你正在使用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 . 从Android O开始,您需要使用https而不是http否则您将收到错误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 您可以将其引用为选择禁止明文流量

Details of documentation: 文件细节:

Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. 注意:本节中的指南仅适用于针对Android 8.1(API级别27)或更低级别的应用。 Starting with Android 9 (API level 28), cleartext support is disabled by default. 从Android 9(API级别28)开始,默认情况下禁用明文支持。

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. 打算仅使用安全连接连接到目标的应用程序可以选择退出支持明文(使用未加密的HTTP协议而不是HTTPS)到这些目的地。 This option helps prevent accidental regressions in apps due to changes in URLs provided by external sources such as backend servers. 此选项有助于防止由于外部源(如后端服务器)提供的URL更改而导致应用中的意外回归。 See NetworkSecurityPolicy.isCleartextTrafficPermitted() for more details. 有关详细信息,请参阅NetworkSecurityPolicy.isCleartextTrafficPermitted()

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. 例如,应用可能希望确保与secure.example.com所有连接始终通过HTTPS完成,以保护敏感流量免受恶意网络的攻击。

res/xml/network_security_config.xml : 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. 您还可以在AndroidManifest.xml使用android:usesCleartextTraffic="true"作为开发模式,但不应在发布模式下使用它。 More details about it in Android Developer Blog , here the excerpts: 有关Android Developer Developer博客的详细信息,请点击此处摘录:

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. 要保护应用程序的已安装基础免受回归到明文流量的影响,请在应用程序的AndroidManifest.xml中的应用程序元素上声明android:usesCleartextTraffic =“false”属性。 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. 这声明应用程序不应该使用明文网络流量,并使Android Marshmallow的平台网络堆栈阻止应用程序中的明文流量。 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. 例如,如果您的应用程序意外尝试通过明文HTTP请求登录用户,则该请求将被阻止,并且用户的身份和密码不会泄漏到网络中。

You don't have to set minSdkVersion or targetSdkVersion of your app to 23 (Android Marshmallow) to use android:usesCleartextTraffic. 您不必将应用程序的minSdkVersion或targetSdkVersion设置为23(Android Marshmallow)以使用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. 请注意,WebView尚未兑​​现此功能。

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. 例如,Socket API忽略明文策略,因为它不知道它发送或接收的数据是否可以归类为明文。 Android platform HTTP stacks, on the other hand, honor the policy because they know whether traffic is cleartext. 另一方面,Android平台HTTP堆栈遵守策略,因为它们知道流量是否明确。

Google AdMob is also built to honor this policy. Google AdMob也是为了遵守此政策而建立的。 When your app declares that it does not use cleartext traffic, only HTTPS-only ads should be served to the app. 当您的应用声明它不使用明文流量时,只应向应用提供仅限HTTPS的广告。

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. 他们可以通过NetworkSecurityPolicy类查询明文流量策略。

http://开头的 URL 您需要在应用程序标签内的清单文件中添加以下属性。

android:usesCleartextTraffic="true"

Add below line of code in AndroidManifest.xml file: 在AndroidManifest.xml文件中添加以下代码行:

android:usesCleartextTraffic="true"

Although, this will give you the warning: 虽然,这会给你警告: 我们添加android时警告:usesCleartextTraffic =“true”

It'll solve the problem for now. 它现在将解决问题。 But you should migrate to 'HTTPS' as soon as possible. 但是您应该尽快迁移到“HTTPS”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM