CertPathValidatorException : Trust anchor for certificate path not found - Retrofit Android

前端 未结 9 2020
庸人自扰
庸人自扰 2020-11-29 16:07

I am creating an android application which uses https for communication with the server. I am using retrofit and OkHttp for making req

9条回答
  •  执笔经年
    2020-11-29 16:37

    If you have a certificate then you can provide but few webservices will not have the certificate for them please follow below.

     // creating a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
    
        // creating a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
    
        // creating an SSLSocketFactory that uses our TrustManager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
        // creating a RestAdapter using the custom client
        return new RestAdapter.Builder()
                .setEndpoint(UrlRepository.API_BASE)
                .setClient(new OkClient(okHttpClient))
                .build();
    

提交回复
热议问题