0

`### I am using retrofit for SSL certification in android . For android version 10 and above i am getting this error as "Unable to extract the trust manager on Android10Platform, sslSocketFactory is class com.android.org.conscrypt.OpenSSLSocketFactoryImpl".For android version 10 below i am getting the resonse, but for 10 and above its not working.

code :

public class Api_Client {

public static final String BASE_URL = "";  // here i am using my base url (i am not mentioned here)

public static Retrofit retrofit;
public static Retrofit getApiClient(Context context) {
InputStream is = null;
SSLContext sslContext = null;
if (retrofit == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
builder.addInterceptor(logging);
}
CertificateFactory cf = null;
try {
is = context.getAssets().open("exapmle.pem");  //certificate in .pem format
cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(is);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
ks.setCertificateEntry("alias", cert);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
builder.readTimeout(30, TimeUnit.SECONDS);
builder.connectTimeout(30, TimeUnit.SECONDS);
builder.sslSocketFactory(sslContext.getSocketFactory());
Retrofit.Builder builder1 =
new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
retrofit = builder1.client(builder.build()).build();

    }
    return retrofit;
}

}

\*\*This code is working for below android version 10. but for above android 10 its not working. is there any solution or any other methods for this issue? \*\*

1 Answers1

0

Issue has been resolved.

We need to use the method

builder.sslSocketFactory(sslContext.getSocketFactory(), systemDefaultTrustManager());
RusArtM
  • 1,116
  • 3
  • 15
  • 22