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

炒沙作縻终不饱,缕冰文章费工巧。这篇文章主要讲述从Android 9(API级别28)开始,默认情况下禁用明文支持。相关的知识,希望能为你提供帮助。
  在生产环境中强制使用https,但在调试模式下允许使用明文的方法  它仅在api 23+上使用
在build.gradle中:

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

在androidManifest.xml中的application标签中
android:usesCleartextTraffic="${usesCleartextTraffic}"

android:usesCleartextTraffic="true"

其它方法“

删除了这一行
android:networkSecurityConfig="@xml/network_security_config"

 
application 添加

android:usesCleartextTraffic="true"

 
改造配置类OKHttp创建时添加了一个连接规范
.connectionSpecs(CollectionsKt.listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))

 
OkHttpClient okHttpClient = new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS) .connectTimeout(10, TimeUnit.SECONDS) .cache(null) .connectionSpecs(CollectionsKt.listOf(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT)) .addInterceptor(new NetworkInterceptor(context)) .addInterceptor(createLoggingInterceptor()) .addInterceptor(createSessionExpiryInterceptor()) .addInterceptor(createContextHeaderInterceptor()) .build();

 
【从Android 9(API级别28)开始,默认情况下禁用明文支持。】 
< network-security-config> < base-config cleartextTrafficPermitted="true"> < trust-anchors> < certificates src="https://www.songbingjia.com/android/system" /> < certificates src="https://www.songbingjia.com/android/user" /> < /trust-anchors> < /base-config> < domain-config cleartextTrafficPermitted="true"> < domain includeSubdomains="true"> api.endv.cn< /domain> < domain includeSubdomains="true"> im.endv.cn< /domain> < domain includeSubdomains="true"> pay.endv.cn< /domain> < domain includeSubdomains="true"> endv.cn< /domain> < /domain-config> < /network-security-config>

 

    推荐阅读