android -------- java.net.UnknownServiceException

 最近升级了Android的API版本时 ,导致我的网络请求失败了,

出现了这个错误 java.net.UnknownServiceException,

这个错误,我在网上查到这个主要是由于,我们的OkHttp3会默认使用密文传输,而我们的代码中使用Http协议,也就是使用明文传输,所以OkHttp3会主动的报错,然后阻止线程的运行。所以我们现在就是要修改配置文件,使OkHttp3允许使用明文传输,或者我们直接使用Https协议。

解决方法:

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

该文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>

    <base-config cleartextTrafficPermitted="true" />

</network-security-config>

然后在 AndroidManifest.xml application 标签内应用上面的xml配置:
 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        android:theme="@style/AppTheme"
        >
</application>

这样就欧克了

官方文档: https://developer.android.com/training/articles/security-config

原文地址:https://www.cnblogs.com/zhangqie/p/10718703.html