AdnroidP OKHTTP3: CLEARTEXT communication to host not permitted by network security policy

1)报错原因:

OKHTTP3 在网络连接时对明文流量的网络请求进行了判断.

在RealConntect类中的方法connect中有这样的判断代码:
 

if (!Platform.get().isCleartextTrafficPermitted(host)) {
  throw new RouteException(new UnknownServiceException(
      "CLEARTEXT communication to " + host + " not permitted by network security policy"));
}
该判断调用的是OKHTTP框架中的AndroidPlatform中的isCleartextTrafficPermitted()方法:

 @Override public boolean isCleartextTrafficPermitted(String hostname) {
  try {
    Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy");
    Method getInstanceMethod = networkPolicyClass.getMethod("getInstance");
    Object networkSecurityPolicy = getInstanceMethod.invoke(null);
    return api24IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy);
  } catch (ClassNotFoundException | NoSuchMethodException e) {
    return super.isCleartextTrafficPermitted(hostname);
  } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
    throw assertionError("unable to determine cleartext support", e);
  }
}

追踪发现:反射调用android系统NetworkSecurityPolicy的isCleartextTrafficPermitted(string host)来取值.
mCleartextTrafficPermitted 在 getDefaultBuilder()中可以看出大于等于Build.VERSION_CODES.P时为false
final boolean cleartextTrafficPermitted = info.targetSdkVersion < Build.VERSION_CODES.P && info.targetSandboxVersion < 2; builder.setCleartextTrafficPermitted(cleartextTrafficPermitted);

2):解决
    1:targetSdkVersion 降级回到 27

    2:改用 https (推荐)

    3:配置xml文件 https://www.cnblogs.com/zdz8207/p/Android-28-http-https.html  (本人未校验)
   

    4: application中添加 android:usesCleartextTraffic="true"
     

在清单配置类 ManifestConfigSource 中: setCleartextTrafficPermitted()对mCleartextTrafficPermitted重新赋值          usesCleartextTraffic.

DefaultConfigSource(boolean usesCleartextTraffic, ApplicationInfo info) {
    mDefaultConfig = NetworkSecurityConfig.getDefaultBuilder(info)
            .setCleartextTrafficPermitted(usesCleartextTraffic)
            .build();
}

     

   

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值