前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android P 第二个测试版本请求网络 CLEARTEXT communication to host not permitted by network

Android P 第二个测试版本请求网络 CLEARTEXT communication to host not permitted by network

作者头像
程思扬
发布2022-01-11 15:06:35
2200
发布2022-01-11 15:06:35
举报
文章被收录于专栏:程思阳的专栏

问题: 由于 Android P 限制了明文流量的网络请求,非加密的流量请求都会被系统禁止掉。

如果当前应用的请求是 htttp 请求,而非 https ,这样就会导系统禁止当前应用进行该请求,如果 WebView 的 url 用 http 协议,同样会出现加载失败,https 不受影响。

为此,OkHttp3 做了检查,所以如果使用了明文流量,默认情况下,在 Android P 版本 OkHttp3 就抛出异常: CLEARTEXT communication to " + host + " not permitted by network security policy

代码语言:javascript
复制
    if (!Platform.get().isCleartextTrafficPermitted(host)) {
          throw new RouteException(new UnknownServiceException(
              "CLEARTEXT communication to " + host + " not permitted by network security policy"));
     }

解决:

1:在 res 下新建一个 xml 目录,然后创建一个名为:network_security_config.xml 文件 ,该文件内容如下:

代码语言:javascript
复制
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true" />
    </network-security-config>

然后在 AndroidManifest.xml application 标签内应用上面的xml配置:

代码语言:javascript
复制
        <application
            android:name=".App"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:networkSecurityConfig="@xml/network_security_config"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:theme="@style/AppTheme"></application>

2:服务器和本地应用都改用 https (推荐)

3:targetSdkVersion 降级回到 27

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档