Android,CLEARTEXT communication not enabled for client

后台接口使用的是http协议,执行http请求时报错:

W/WebSocketConnection: onFailure()
    java.net.UnknownServiceException: CLEARTEXT communication not enabled for client
        at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:143)
        at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)
        at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
        at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at org.thoughtcrime.securesms.net.UserAgentInterceptor.intercept(UserAgentInterceptor.java:20)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:201)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

原因:
从Android 9(API级别28)开始,默认情况下禁用明文传输支持。

解决:在创建OkHttpClient的时候添加ConnectionSpec.CLEARTEXT的连接规范配置,如:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectionSpecs(Arrays.asList(ConnectionSpec.CLEARTEXT))
.build();

然后执行时,报错:

W/System.err: Caused by: java.net.UnknownServiceException: CLEARTEXT communication to 13.229.209.248 not permitted by network security policy
W/System.err:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:148)
W/System.err:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:258)
W/System.err:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
W/System.err:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:127)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at org.thoughtcrime.securesms.net.UserAgentInterceptor.intercept(UserAgentInterceptor.java:20)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:257)
W/System.err:     at okhttp3.RealCall.execute(RealCall.java:93)
W/System.err:     at org.whispersystems.signalservice.internal.push.PushServiceSocket.getWalletServiceConnection(PushServiceSocket.java:1578)

解决:
AndroidManifest.xml文件中的 <application> 标签中添加 android:usesCleartextTraffic="true" 属性,比如:

    <application android:name=".ApplicationContext"
                 android:icon="@mipmap/ic_launcher"
                 android:label="@string/app_name"
                 android:supportsRtl="true"
                 tools:replace="android:allowBackup"
                 android:allowBackup="false"
                 android:theme="@style/TextSecure.LightTheme"
                 android:largeHeap="true"
                 android:usesCleartextTraffic="true">

再次发起http请求,可以正常执行。

如何在生产环境只使用https,但在debug模式可以使用http

如何在生产环境中只使用https,不可以使用http,但在debug调试模式下允许使用明文?
在build.gradle中配置:

// Put this in your buildtypes debug section:
manifestPlaceholders = [usesCleartextTraffic:"true"]

// Put this in your buildtypes release section
manifestPlaceholders = [usesCleartextTraffic:"false"]

参考:
【Android】【网络请求】解决cleartext communication not permitted问题
使用OkHttp3在Android P 出现的错误:CLEARTEXT communication to host not permitted by network

OKHTTP 错误CLEARTEXT communication not supported

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值