0

I want to fetch html page source from URL. Retrofit is doing the job on post Lollipop devices. But on pre-Lollipop devices it gives unsupported protocol error.

Code :

private static final String TAG = "TESTTESTTESTTEST";
private static String url = "https://www.hepsiburada.com/lassa-235-65r17-108h-xl-competush-" +
            "l-p-OTLST216410?magaza=LastikArt%C4%B1&utm_source=pc&utm_medium=cimri&utm_campaign" +
            "=c&utm_content=c&utm_term=5083&wt_pc=cimri.c.5083.pc/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Retrofit retrofit = new Retrofit.Builder()
            .addConverterFactory(ScalarsConverterFactory.create())
            .baseUrl("https://www.google.com/")
            .build();

    IGetHtml iGetHtml = retrofit.create(IGetHtml.class);
    Call<String> stringCall = iGetHtml.getHtml(url);
    stringCall.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            if (response.isSuccessful()) {
                String responseString = response.body();
                Log.e(TAG, "onResponse: " + response.body());
            }
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.e(TAG, "onFailure: " + t);
        }
    });


}

Error

E/NativeCrypto: Unknown error during handshake 07-09 12:23:57.606

E/TESTTESTTESTTEST:onFailure:javax.net.ssl.SSLHandshakeException:javax.ne.ssl.SSLProtocolException: SSL handshake aborted:ssl=0x7cf6f320: Failure in SSL library, usually a protocol error error:14077102:SSL routines:SSL23_GET_SERVER_HELLO:unsupported protocol (external/openssl/ssl/s23_clnt.c:714 0x7a084894:0x00000000)

Naitik Soni
  • 700
  • 8
  • 16
Emil Mammadov
  • 380
  • 4
  • 20

2 Answers2

2

I solved my error with this solution.

Everything is the same as I wrote in the question except this changes.

Add this class to your project.

public class TLSSocketFactory extends SSLSocketFactory {

    private SSLSocketFactory delegate;

    public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, null, null);
        delegate = context.getSocketFactory();
    }

    @Override
    public String[] getDefaultCipherSuites() {
        return delegate.getDefaultCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        return delegate.getSupportedCipherSuites();
    }

    @Override
    public Socket createSocket() throws IOException {
        return enableTLSOnSocket(delegate.createSocket());
    }

    @Override
    public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
        return enableTLSOnSocket(delegate.createSocket(s, host, port, autoClose));
    }

    @Override
    public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
        return enableTLSOnSocket(delegate.createSocket(host, port));
    }

    @Override
    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
        return enableTLSOnSocket(delegate.createSocket(host, port, localHost, localPort));
    }

    @Override
    public Socket createSocket(InetAddress host, int port) throws IOException {
        return enableTLSOnSocket(delegate.createSocket(host, port));
    }

    @Override
    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
        return enableTLSOnSocket(delegate.createSocket(address, port, localAddress, localPort));
    }

    private Socket enableTLSOnSocket(Socket socket) {
        if (socket != null && (socket instanceof SSLSocket)) {
            ((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2"});
        }
        return socket;
    }
}

And then onCreate method changes to this.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        OkHttpClient client = new OkHttpClient();
        try {
            client = new OkHttpClient.Builder()
                    .sslSocketFactory(new TLSSocketFactory())
                    .build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://www.google.com/")
                .client(client)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();

        IGetHtml iGetHtml = retrofit.create(IGetHtml.class);
        Call<String> stringCall = iGetHtml.getHtml(url);
        stringCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()) {
                    Log.e(TAG, "onResponse: " + response.headers());
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Log.e(TAG, "onFailure: " + t);
            }
        });


    }
Emil Mammadov
  • 380
  • 4
  • 20
1

It is not because of Retrofit but because of TLS and SSL support not being enabled for lower versions of Android.

enter image description here

https://developer.android.com/reference/javax/net/ssl/SSLSocket

You can enable TLS on Android version 4.1 - 4.4 by checking for Google Play Service update in your app. You can make a separate search for ProviderInstaller in Android.

If its just an html page I would recommend just opening it in the external browser by using

new Intent(Intent.ACTION_VIEW, Uri.parse("your url"));
Gelo
  • 320
  • 2
  • 9