package comexampleretrofittestimport androidxappcompatappAppCompatActivityimport retrofit2Callimport androidosBundleimport androidutilLogimport comexampleretrofittestdatabindingActivityMainBindingimpo
这个错误是由于网络安全策略导致的。在Android 9及以上的版本中,默认禁止使用明文通信(http),只允许使用加密通信(https)。
解决方法有两种:
-
推荐的做法是将服务器端的地址改为使用https协议,这样可以保证通信的安全性。需要在服务器端配置SSL证书。
-
如果不想使用https协议,可以在AndroidManifest.xml文件中添加以下代码:
<application
...
android:usesCleartextTraffic="true"
...>
这样可以允许在Android 9及以上版本上使用明文通信。
注意:在正式发布应用时,建议使用第一种方式,使用https协议来保证通信的安全性。第二种方式只用于调试和开发阶段,不建议在正式发布的应用中使用。
原文地址: https://www.cveoy.top/t/topic/iJx2 著作权归作者所有。请勿转载和采集!