HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to "包名"

本文介绍了解决Android P系统中默认禁止HTTP传输的问题。通过配置network_security_config.xml文件并启用cleartextTrafficPermitted参数,使得应用可以在不使用HTTPS的情况下继续运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在把测试机的版本升级到9之后,原来的接口报错

HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to "包名" not permitted by
复制代码

Android P默认是不允许使用明文网络传输的,会强制让应用都使用https

经过网上查证,其实还有可以兼容http的方法

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

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

然后在清单文件的application标签内加上:

android:networkSecurityConfig="@xml/network_security_config"
复制代码

转载于:https://juejin.im/post/5ca1e09ef265da30db494f28

Api Version : 0x00401000 2025-06-10 20:56:03.423 11873-11873 url com.example.kucun2 D loadAllData: http://198.162.31.177:3000/app/all 2025-06-10 20:56:03.433 11873-18742 DataLoader com.example.kucun2 E Failed to load data java.net.UnknownServiceException: CLEARTEXT communication to 198.162.31.177 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) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201) at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) at java.lang.Thread.run(Thread.java:1042) 2025-06-10 20:56:03.500 11873-11873 .example.kucun2 com.example.kucun2 W Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphic
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值