Android api27以上的sdk为了安全性,禁止使用http请求,而要求使用https
但是使用https需要对服务端有一定要求,使用起来比较麻烦,所以我们找一个方法可以使用http请求

新增一个xml目录

在资源文件夹 res 下新增一个 xml 目录,然后创建一个名为:network_security_config.xml 文件(名字自定) ,内容如下,大概意思就是允许开启http请求

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <network-security-config>
  3. <base-config cleartextTrafficPermitted="true" />
  4. </network-security-config>
  1. 然后在APPAndroidManifest.xml文件下的application标签增加以下属性
  1. <application
  2. ...
  3. android:networkSecurityConfig="@xml/network_security_config"
  4. ...
  5. />

其他方法

  • APP改用https请求
  • targetSdkVersion 降到27以下

    参考资料

    链接