Android Retrofit 无法连接 HTTP 服务器:解决 "CLEARTEXT communication" 错误
{
"title": "Android Retrofit 无法连接 HTTP 服务器:解决 "CLEARTEXT communication" 错误",
"description": "本文介绍了 Android 开发中使用 Retrofit 库连接 HTTP 服务器时遇到的 "CLEARTEXT communication not permitted" 错误,并提供了解决方案,包括使用 HTTPS 协议和在 AndroidManifest.xml 中配置 android:usesCleartextTraffic 属性。",
"keywords": "Android, Retrofit, HTTP, CLEARTEXT communication, 网络安全策略, HTTPS, SSL证书, AndroidManifest.xml",
"content": "这个错误是由于网络安全策略导致的。在 Android 9 及以上的版本中,默认禁止使用明文通信(http),只允许使用加密通信(https)。\n\n解决方法有两种:\n\n1. 推荐的做法是将服务器端的地址改为使用 https 协议,这样可以保证通信的安全性。需要在服务器端配置 SSL 证书。\n\n2. 如果不想使用 https 协议,可以在 AndroidManifest.xml 文件中添加以下代码:\n\nxml\n<application\n ...\n android:usesCleartextTraffic="true"\n ...\n>\n\n\n这样可以允许在 Android 9 及以上版本上使用明文通信。\n\n注意:在正式发布应用时,建议使用第一种方式,使用 https 协议来保证通信的安全性。第二种方式只用于调试和开发阶段,不建议在正式发布的应用中使用。"
}
原文地址: https://www.cveoy.top/t/topic/qpPT 著作权归作者所有。请勿转载和采集!