解决安卓OkHttp报错UnknownServiceException CLEARTEXT communication to not permitted by network security

报错时的运行环境:

问题简述:

用android利用okhttp像本地发送请求时报错

 Caused by: java.net.UnknownServiceException: CLEARTEXT communication to localhost not permitted by network security policy

由于java.net.UnknownServiceException:不允许通过网络安全策略进行与localhost的明文通信

Process: com.catanddog, PID: 21453
                 java.lang.RuntimeException: java.net.UnknownServiceException: CLEARTEXT communication to localhost not permitted
                 by network security policy
                 	at com.catanddog.LoginActivity$1$1.run(LoginActivity.java:83)
                 	at java.lang.Thread.run(Thread.java:764)
                 Caused by: java.net.UnknownServiceException: CLEARTEXT communication to localhost not permitted by network
                 security policy

详情可见安卓中文官网描述

问题解决:(三种方法)

1. 使用安全的加密传输方式:改用HTTPS或其他加密方式来与本地主机进行通信,以符合网络安全策略的要求。

2. 修改网络安全策略:如果可能,您也可以尝试修改网络安全策略以允许明文通信,但这并不是推荐的做法,因为会增加安全风险。

android:usesCleartextTraffic="true"

3.配置网络安全策略文件:从 Android 9(API 级别 28)开始,Android 引入了网络安全策略机制,您需要在项目中配置 network_security_config.xml 文件,以允许明文通信。您可以创建一个 network_security_config.xml 文件并将其添加到 res/xml 目录下,并在 AndroidManifest.xml 文件中引用它。以下是一个示例配置文件:

```xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
```

 在 OkHttp 客户端中设置允许明文通信:在创建 OkHttpClient 实例时,使用 clearTextTraffic() 方法来设置允许明文通信,示例代码如下:

```java
OkHttpClient client = new OkHttpClient.Builder()
.clearTextTraffic()
.build();
``` 

  • 19
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
E/AndroidRuntime: FATAL EXCEPTION: Thread-18 Process: com.example.read, PID: 22568 java.lang.RuntimeException: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:111) at java.lang.Thread.run(Thread.java:920) Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 192.168.210.113 not permitted by network security policy at okhttp3.internal.connection.RealRoutePlanner.planConnectToRoute$okhttp(RealRoutePlanner.kt:195) at okhttp3.internal.connection.RealRoutePlanner.planConnect(RealRoutePlanner.kt:152) at okhttp3.internal.connection.RealRoutePlanner.plan(RealRoutePlanner.kt:67) at okhttp3.internal.connection.FastFallbackExchangeFinder.launchTcpConnect(FastFallbackExchangeFinder.kt:118) at okhttp3.internal.connection.FastFallbackExchangeFinder.find(FastFallbackExchangeFinder.kt:62) at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:267) 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:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) at okhttp3.internal.connection.RealCall.execute(RealCall.kt:158) at com.example.read.upload_serverActivity$1.run(upload_serverActivity.java:106) at java.lang.Thread.run(Thread.java:920) 怎么解决
05-29

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值