互联网许可不适用于奥利奥和馅饼

Internet permission not working in oreo and pie

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

该应用程序在 android N 以下的设备上运行良好。但不适用于 O 和 P。似乎问题出在互联网许可上。我已经检查过,应用程序在 o 和 p 上为 运行 时未向服务器发送任何数据。如果我需要寻求任何特定的互联网访问许可,请告诉我。

我已经在清单中添加了这个

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

如有任何帮助,我们将不胜感激。

这可能是因为您正在使用 http。从AndroidO开始,需要用https代替http,否则会报错Cleartext HTTP traffic to * not permitted。因此,您需要创建一个配置来允许这样做。可以参考Opt out of cleartext traffic

文档详情:

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()了解更多详情。

例如,应用程序可能希望确保所有连接到 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>

.


您也可以在 AndroidManifest.xml 中使用 android:usesCleartextTraffic="true" 作为您的开发模式,但您不应在发布模式中使用它。 Android Developer Blog 中有更多详细信息,摘录如下:

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.

如果您的 URL 以 http 开头,那么您必须使用它用于 Android Pie 即 API 级别 28

android:usesCleartextTraffic="true"

在您的应用程序标签内的清单中 作为属性

在 AndroidManifest.xml 文件中添加以下代码行:

android:usesCleartextTraffic="true"

不过,这会给您警告:

暂时解决问题。但是您应该尽快迁移到 'HTTPS'。

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

android:usesCleartextTraffic="true"