报错如下

Process: com.wisdom.pay, PID: 14426 java.net.UnknownServiceException: CLEARTEXT communication to 192.168.31.152 not permitted by network security policy
at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:188)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)

原因分析

安卓为了安全考虑,默认不支持http请求支持https请求。我开发时用的局域网的IP,还是http方便。

解决办法

  1. 使用https 推荐在生产环境中使用。
  2. 配置仅限,允许http请求。(不推荐在生产环境中使用)

您还可以将AndroidManifest.xml中的android:usesCleartextTraffic属性设置为“true”。这将允许您的应用进行的所有连接使用明文通信。但是,这是一种不太安全的方法,仅应作为最后手段使用。

<application
    ...
    android:usesCleartextTraffic="true"
    ...>
</application>

请记住,使用明文通信会使您的应用暴露于潜在的安全漏洞。始终建议尽可能切换到HTTPS以确保安全的数据传输。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐