Android NoConnectionError: java.io.IOException: Cleartext HTTP traffic to xxx not permitted 解决办法

问题原因是:

Google表示,为保证用户数据和设备的安全,针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。

解决办法:

1,采用HTTPS协议,就避免了未加密的请求。

2,targetSdkVersion降到27以下。

3,增加网络安全配置xml。

在 res 下新增一个 xml 目录,创建一个名为:urls_config.xml 文件(名字也可以自定) ,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

在APP的AndroidManifest.xml文件下的application标签增加以下属性:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxxx.xxx">

    <uses-permission android:name="android.permission.INTERNET" />
<application
        ...
    android:networkSecurityConfig="@xml/urls_config"
        ...
    >
</manifest>

4,在Application标签下添加<code>android:usesCleartextTraffic属性.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxxx.xxx">

    <uses-permission android:name="android.permission.INTERNET" />
<application
        ...
    android:usesCleartextTraffic="true"
        ...
    >
</manifest>

猜你喜欢

转载自blog.csdn.net/NewActivity/article/details/121212697
今日推荐