11

In my application I want get data from server and show this into RecyclerView.
For get data from server I use Retrofit2 and I write below codes.
But when running application after some time show me E/priceResLog: Err : SSL handshake timed out in onFailure from Retrofit2!

My codes :

public class ApiUtilsPrice {

    private static final String BASE_URL = "https://core.arzws.com/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .build();

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

Activity codes :

private void getCoinData() {
    loaderLayout(true);
    Call<GoldListResponse> call = apiInterface.getGoldPrice();
    call.enqueue(new Callback<GoldListResponse>() {
        @Override
        public void onResponse(Call<GoldListResponse> call, Response<GoldListResponse> response) {
            if (response.isSuccessful()) {
                if (response.body() != null) {
                    loaderLayout(false);
                    model.clear();
                    model.addAll(response.body().getGoldBoard());
                    coinAdapter.notifyDataSetChanged();
                    isSendApi = true;
                }
            }
        }

        @Override
        public void onFailure(Call<GoldListResponse> call, Throwable t) {
            Log.e("priceResLog", "Err : " + t.getMessage());
        }
    });
}

How can I fix it? please help me thanks.

9
  • increase timeout 60 to 90. Jun 22, 2018 at 5:57
  • @HemantParmar, why?
    – WoW.j
    Jun 22, 2018 at 5:58
  • issue with server. chain your certificate properly.no need to change in android side. Jun 22, 2018 at 5:59
  • You need to add default certificate to retrofit
    – ManishNegi
    Jun 22, 2018 at 5:59
  • don't add static certificates. Jun 22, 2018 at 5:59

1 Answer 1

8

Add the following piece of code

public Retrofit getRetrofit(Gson gson) {
            return new Retrofit.Builder()
                    .baseUrl(ZoneApplication.getContext().getString(R.string.feed_data_base_url))
                    .client(HttpClientService.getUnsafeOkHttpClient())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .addConverterFactory(new NullOnEmptyConverterFactory())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();

        }

or change your code to

public static Retrofit getClient() {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .build();

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(HttpClientService.getUnsafeOkHttpClient())
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

Create another class called HttpClientService and add the following code

public class HttpClientService {
    public static OkHttpClient getUnsafeOkHttpClient() {

        try {
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @SuppressLint("TrustAllX509TrustManager")
                        @Override
                        public void checkClientTrusted(
                                java.security.cert.X509Certificate[] chain,
                                String authType) {
                            //Do nothing
                        }

                        @SuppressLint("TrustAllX509TrustManager")
                        @Override
                        public void checkServerTrusted(
                                java.security.cert.X509Certificate[] chain,
                                String authType) {
                            //Do nothing
                        }

                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[0];
                        }
                    }};
            final SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts,
                    new java.security.SecureRandom());

            final SSLSocketFactory sslSocketFactory = sslContext
                    .getSocketFactory();

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .sslSocketFactory(sslSocketFactory)
                    .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                    .build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
4
  • Can you send to me your Skype ID? please
    – WoW.j
    Jun 22, 2018 at 6:09
  • 1
    I do not understand, what is use of creating okHttpClient instance, if we are passing HttpClientService.getUnsafeOkHttpClient() as a client? Sep 12, 2019 at 5:36
  • It has really fixed the https connection on old android. Thank you! Nov 18, 2019 at 10:08
  • @deprecated [SSLSocketFactory], is any alternate syntax?
    – Bolt UIX
    May 11, 2021 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.