Android 9.0 HTTP请求报错

主要参考stackoverflow上hrishikesh-kadam的回答

1。在res创建xml文件夹创建network_security_config.xml



    
        (你的http url如我写的)10.0.70.43
    

    
    
        
            
            
        
    

2。在AndroidManifest.xml中添加下面代码

android:networkSecurityConfig="@xml/network_security_config"

3。我的https和http互换添加的一些

//下面代码看https://github.com/square/okhttp/wiki/HTTPS
        //https用ConnectionSpec.MODERN_TLS,不是的就用ConnectionSpec.CLEARTEXT
        ConnectionSpec spec = null;
        if (BASEURL.startsWith("https")) {
            spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                    .tlsVersions(TlsVersion.TLS_1_3,
                            TlsVersion.TLS_1_2)
                    .cipherSuites(
                            CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
                            CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
                    .build();
        } else {
            spec = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT)
                    .build();
        }

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(你自定义的sslParams.sSLSocketFactory, 你自定义的sslParams.trustManager)
                .connectionSpecs(Collections.singletonList(spec))
                .hostnameVerifier(你自定义的new HttpsUtils.UnSafeHostnameVerifier())

你可能感兴趣的:(Android 9.0 HTTP请求报错)