I am using retrofit for SSL certification in Android. For Android version 10 and above, I am getting this error: "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 response, but for 10 and above, it's 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 versions below Android 10. But for versions above Android 10, it's not working. Is there any solution or any other method for this issue?
Issue has been resolved.
We need to use the method