sslSocketFactory()' in 'okhttp3.OkHttpClient' cannot be applied to '(javax.net.ssl.SSLSocketFactory)'

2.4k Views Asked by At

I am implementing Certificate Pinning in My App. My app uses Retrofit for Web Services. Here is my code. I am getting

sslSocketFactory()' in 'okhttp3.OkHttpClient' cannot be applied to '(javax.net.ssl.SSLSocketFactory)'

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.skill.project.os.R;

import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

class ServiceGenerator {

    /**
     * Generates an OkHttpClient with our trusted CAs
     * to make calls to a service which requires it.
     *
     * @param context the context to access our file.
     * @return OkHttpClient with our trusted CAs added.
     */
    private static OkHttpClient generateSecureOkHttpClient(Context context) {

        try {
            // Create a simple builder for our http client, this is only por example purposes
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient httpClientBuilder = new OkHttpClient.Builder()
                    .readTimeout(60, TimeUnit.SECONDS)
                    .connectTimeout(60, TimeUnit.SECONDS).addInterceptor(logging).build();
            ;

            // Here you may wanna add some headers or custom setting for your builder

            // Get the file of our certificate
            InputStream caFileInputStream = context.getResources().openRawResource(R.raw.satta_us);

            // We're going to put our certificates in a Keystore
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(caFileInputStream, "qwerty@123".toCharArray());

            // Create a KeyManagerFactory with our specific algorithm our our public keys
            // Most of the cases is gonna be "X509"
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
            keyManagerFactory.init(keyStore, "qwerty@123".toCharArray());

            // Create a SSL context with the key managers of the KeyManagerFactory
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

            //Finally set the sslSocketFactory to our builder and build it
            return httpClientBuilder
                    

// Here I am getting Error .sslSocketFactory(sslContext.getSocketFactory()) sslSocketFactory()' in 'okhttp3.OkHttpClient' cannot be applied to '(javax.net.ssl.SSLSocketFactory)'enter image description here

                    .sslSocketFactory(sslContext.getSocketFactory())
                    .build();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * Example method to show the usage of the OkHttpClient
     *
     * @param context the context to access our file.
     * @return Retrofit service
     */
    public static Retrofit generateService(Context context) {
        Gson gson = new GsonBuilder().setLenient().create();
        return new Retrofit.Builder()
                .baseUrl(RetroApp.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(generateSecureOkHttpClient(context))
                .build();
    }

}

Please help me.

0

There are 0 best solutions below