1

i have an API that uses SLL certification when i open api link google chrome ask me for login with username and password to authorized me to connect with website API Link : https://45.55.43.15:9090/api/machine?page=0&size=10

My question is how can implement this using OkHttp3 and Retrofit2

here is my trial

 public class RestAdapter {


    Context context;
    public static final String BASE_URL = "https://45.55.43.15:9090/api/";


    private OkHttpClient okHttpClient;
    private Authenticator authenticator = new Authenticator() {
        @Override
        public Request authenticate(Route route, Response response) {
            return null;
        }
    };


    private RestAdapter() {
    }

    public void setAuthenticator(Authenticator authenticator) {
        this.authenticator = authenticator;
    }


    public static class Builder {
        String email, password;
        RestAdapter apiManager = new RestAdapter();

        public Builder setAuthenticator(Authenticator authenticator) {
            apiManager.setAuthenticator(authenticator);
            return this;
        }

        public RestAdapter build(String param_email, String param_password) {
            this.email = param_email;
            this.password = param_password;
            return apiManager.newInstance(email, password);
        }

    }

    public class RequestTokenInterceptor implements Interceptor {
        String email, password;
        String credentials, basic;
        public RequestTokenInterceptor(String email, String password) {
            this.email = email;
            this.password = password;
            credentials = email + ":" + password;
            basic = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
        }



        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder()
                    .addHeader("Authorization", basic)
                    .method(original.method(), original.body());
            return chain.proceed(builder.build());
        }
    }

    private RestAdapter newInstance(String email, String password) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.e("https", message);
            }
        });
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);


        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .addInterceptor(new RequestTokenInterceptor(email, password))
                .authenticator(authenticator)
                .build();


        return this;
    }


    public <T> T createRest(Class<T> t) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        return retrofit.create(t);
    }
 }

1 Answers1

0

That server is using a self-signed certificate. Web browsers will refuse to connect to it as a result, for example.

Either use network security configuration or OkHttp's custom certificate support to allow your code to connect to this server. Or, switch to a server with a domain name and a standard SSL certificate.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • i know the server using self-signed certificate, but my question is how can i solve this problem with Retrofit2, i need some example to write right code thanks – Khaled Mohammad Jul 15 '18 at 15:46
  • @KhaledMohammad: "how can i solve this problem with Retrofit2" -- as I wrote, either use network security configuration or OkHttp's custom certificate support to allow your code to connect to this server. You can supply an `OkHttpClient` to Retrofit, where you have configured that `OkHttpClient` to support your self-signed certificate. See [this](https://stackoverflow.com/q/36552316/115145) and [this](https://codelabs.developers.google.com/codelabs/android-network-security-config/index.html) for more on the network security configuration solution. – CommonsWare Jul 15 '18 at 16:37