okhttp3 Unable to load publicsuffixes.gz resource from the classpath

本文介绍了一个关于HTTPFailed异常的问题,该异常与OkHttp中的PublicSuffixDatabase有关。文章提供了具体的解决方案,即通过在proguard-rules.pro文件中添加特定规则来避免混淆导致的问题。

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

HTTP FAILED: java.lang.IllegalStateException: Unable to load publicsuffixes.gz resource from the classpath

项目分插件和宿主,插件单独能够运行。把插件放到宿主里面就报上面这个错误。多半是因为把PublicSuffixDatabase混淆了,

在proguard-rules.pro添加下面两行即可

-keep class okhttp3.internal.publicsuffix.PublicSuffixDatabase
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

 

 

后记:

发生这个错误的 前一天还是好好的,所以发生错误后一开始没有往混淆上面想,查了好长时间后来对比发现,之前不混淆okhttp3的不知道啥时候不见了。。。。

其实,避免这些第三方库的配置一开始就要加上去-keep class okhttp3.**{*;}
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
<think>我们遇到了一个网络请求错误:CLEARTEXTcommunicationto198.162.31.177notpermittedbynetworksecuritypolicy。这是因为Android9(API级别28)及以上版本默认禁止明文传输(HTTP),而我们的目标地址是HTTP(非HTTPS)。要解决此问题,我们需要在网络安全配置中允许明文传输。解决方案:1.在res/xml目录下创建network_security_config.xml文件,配置允许明文传输。2.在AndroidManifest.xml中引用此网络安全配置。3.如果目标域名是已知的,可以只针对该域名允许明文传输;但这里我们使用IP地址,所以可以允许所有明文传输(仅建议在开发环境使用)。步骤:步骤1:创建network_security_config.xml文件在res/xml目录下(如果不存在则创建xml目录)创建network_security_config.xml文件,内容如下:```xml<?xmlversion="1.0"encoding="utf-8"?><network-security-config><domain-configcleartextTrafficPermitted="true"><!--允许所有域名使用明文HTTP,包括IP地址--><domainincludeSubdomains="true">198.162.31.177</domain></domain-config><!--或者为了允许所有明文传输(不推荐),可以这样写:--><!--<base-configcleartextTrafficPermitted="true"/>--></network-security-config>```注意:上面的配置只允许特定IP地址的明文传输,相对安全一些。如果将来域名变化,需要调整。步骤2:在AndroidManifest.xml的application标签中设置网络安全配置```xml<application...android:networkSecurityConfig="@xml/network_security_config"...>...</application>```步骤3:重新运行应用,应该可以成功访问HTTP地址。如果上述步骤不起作用,或者需要临时允许所有明文传输(仅用于测试),可以使用:```xml<?xmlversion="1.0"encoding="utf-8"?><network-security-config><base-configcleartextTrafficPermitted="true"/></network-security-config>```但请注意,允许所有明文传输在正式发布的应用中是不安全的,所以只建议在开发阶段使用。此外,还可以尝试另一种方法(不推荐,因为从Android9开始,即使设置usesCleartextTraffic为true,还需要网络安全配置):在AndroidManifest.xml的application标签中设置:android:usesCleartextTraffic="true"。但是,如果同时设置了networkSecurityConfig,则以网络安全配置为准。因此,我们可以这样设置:```xml<application...android:usesCleartextTraffic="true"...>...</application>```但请注意,这种方法在Android9及以上可能会被忽略,所以最好的方式还是使用网络安全配置。然而,根据日志,目标地址是IP地址(198.162.31.177),我们已经在网络安全配置中允许了该IP地址的明文传输。所以应该可以工作。如果问题依然存在,请检查设备上的时间设置(HTTPS需要正确的时间)以及网络连接情况。但这里使用的是HTTP,所以时间设置不影响,主要是网络连接和配置。补充:在较新的Android版本中,必须明确指定允许的域名或IP地址。我们这里指定了IP地址,所以应该是有效的。重新运行应用,网络请求应该能够成功。另外,如果是在Android9以下的设备上运行,可能不需要这个配置,但为了兼容新设备,我们应该始终配置网络安全。§§相关问题§§:1.如何在Android中配置网络安全以允许特定的HTTP域名?2.在Android开发中,如何避免因使用明文HTTP而引发的安全风险?3.除了配置网络安全,还有哪些方法可以解决CLEARTEXTcommunicationnotpermitted的问题?4.如何为Android应用配置自定义证书?</think> 根据日志信息,您的应用遇到了网络安全策略问题: ``` java.net.UnknownServiceException: CLEARTEXT communication to 198.162.31.177 not permitted by network security policy ``` 这表示您的应用尝试使用 HTTP(明文协议)连接到服务器 `198.162.31.177`,但 Android 9+ 默认禁止了明文网络请求。以下是完整的解决方案: ### 解决方案:允许明文 HTTP 通信 #### 方法一:创建网络安全配置文件(推荐) 1. 在 `res/xml` 目录下创建 `network_security_config.xml` 文件: ```xml <?xml version="1.0" encoding="utf-8"?> <network-security-config> <!-- 允许到特定 IP 的明文通信 --> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">198.162.31.177</domain> </domain-config> <!-- 或者允许所有明文通信(不推荐) --> <!-- <base-config cleartextTrafficPermitted="true" /> --> </network-security-config> ``` 2. 在 `AndroidManifest.xml` 中引用这个配置: ```xml <application android:networkSecurityConfig="@xml/network_security_config" ...> ... </application> ``` #### 方法二:修改 AndroidManifest 设置(兼容旧版 Android) 在 `AndroidManifest.xml` 的 `<application>` 标签中添加: ```xml <application android:usesCleartextTraffic="true" ...> ... </application> ``` #### 方法三:调试中使用 adb 命令(临时解决方案) 在调试设备上运行: ```bash adb shell settings put global ntp_server google.com adb shell settings put global captive_portal_detection_enabled 0 ``` ### 完整示例代码 #### network_security_config.xml ```xml <?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">198.162.31.177</domain> <domain includeSubdomains="true">您的域名.com</domain> </domain-config> </network-security-config> ``` #### AndroidManifest.xml 更新 ```xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.kucun2"> <uses-permission android:name="android.permission.INTERNET"/> <application android:networkSecurityConfig="@xml/network_security_config" android:usesCleartextTraffic="true" ...> <!-- 其他内容 --> </application> </manifest> ``` ### 解释说明 1. **Android 9+ 安全策略**: - 从 Android 9 (API 28) 开始,默认禁止 HTTP 明文通信 - 此限制旨在防止中间人攻击 - 影响所有使用 `HttpURLConnection`、`OkHttp` 等 HTTP 客户端的情况 2. **最佳实践**: - 生产环境应使用 HTTPS - 调试环境使用上述方法一允许特定域名/IP - 避免使用 `android:usesCleartextTraffic="true"` 开启所有明文通信 3. **工作原理**: - `network_security_config.xml` 定义白名单 - `@xml/network_security_config` 应用配置到整个应用 - 指定域名/IP 允许其 HTTP 通信 ###
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值