解决 Android 28 不能请求 HTTP 接口的问题

  • Android,API28,HTTP,HTTPS

从Android 9(API级别28)开始,默认情况下禁用明文支持。出发点是好的,能用 https 的尽量全应用,全站 https 化,如果我们需要一个过渡期怎么办,两个方案

方案一 配置 manifest 文件

代码:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="xxxx">
    <application
      ...
      android:usesCleartextTraffic="true"    // 添加这一行
      ...
      >
      ...
  </application>
</manifest>

在 manifest 文件中增加 android:usesCleartextTraffic="true" 属性。优点和缺点就是作用于整个应用。

方案二 network_security_config 网络安全配置

在 manifest 中增加

  <application
  ...
  android:networkSecurityConfig="@xml/network_security_config"
  ...
  >
  </application>

然后在 network_security_config 文件中增加配置:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">需要支持 HTTP 的域名</domain>
    </domain-config>
</network-security-config>

该方案可以精确的配置某些域名,特别是第三方库里的域名。

阅读原文 : 解决 Android 28 不能请求 HTTP 接口的问题 https://github.com/collinxz-coder/blog/issues/5

相关文章

- EOF -

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处,尊重他人劳动。
转载请注明:文章转载自 Binkery 技术博客 [https://binkery.com]
本文标题: 解决 Android 28 不能请求 HTTP 接口的问题
本文地址: https://binkery.com/archives/2019.09.30-Android-29-不能使用HTTP请求.html