0

I am trying to get data from my website using Retrofit. It's working fine from Android 5.0 but lesser android version showing error message Connection closed by peer. Here is my code...

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://myWebsite.com/api/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
service = retrofit.create(MyService.class);

And here is MyService class

@GET
Call<CategoryResponse> getCategoryResponse(@Url String url);

What am I missing here? It's completely working fine over Android 5.0. I think it is something to deal with SSL Handshake and OkHttpClient. I don't know how to implement OkHttpClient with Retrofit.

Here is my logcat

 06-29 10:50:49.906 10438-10438/com.dualbrotech.playwithprizes E/dalvikvm:       Could not find class 'android.support.v4.widget.DrawerLayout$1', referenced  from method android.support.v4.widget.DrawerLayout.<init>
06-29 10:50:49.914 10438-10438/com.dualbrotech.playwithprizes E/dalvikvm:  Could not find class 'android.view.WindowInsets', referenced from method  android.support.v4.widget.DrawerLayout.onDraw
06-29 10:50:49.917 10438-10438/com.dualbrotech.playwithprizes E/dalvikvm:  Could not find class 'android.view.WindowInsets', referenced from method  android.support.v4.widget.DrawerLayout.onMeasure
06-29 10:50:49.918 10438-10438/com.dualbrotech.playwithprizes E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.widget.DrawerLayout.onMeasure
06-29 10:50:51.219 10438-10611/com.dualbrotech.playwithprizes   E/NativeCrypto: Unknown error during handshake
06-29 10:50:52.194 10438-10615/com.dualbrotech.playwithprizes E/NativeCrypto: ssl=0x541654b8 cert_verify_callback x509_store_ctx=0x540adab8 arg=0x0
ssl=0x541654b8 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
06-29 10:50:52.254 10438-10618/com.dualbrotech.playwithprizes E/NativeCrypto: Unknown error during handshake
06-29 10:50:52.296 10438-10438/com.dualbrotech.playwithprizes E/error: javax.net.ssl.SSLException: Connection closed by peer
06-29 10:51:31.769 10438-10618/com.dualbrotech.playwithprizes E/NativeCrypto: Unknown error during handshake
06-29 10:51:35.491 10438-10618/com.dualbrotech.playwithprizes E/NativeCrypto: Unknown error during handshake
06-29 10:51:35.503 10438-10438/com.dualbrotech.playwithprizes E/error: javax.net.ssl.SSLException: Connection closed by peer
  • you are missing your `service.getCategoryResponse.enqueue(Call)`? – HawkPriest Jun 28 '18 at 13:30
  • no, it's working fine from android 5.0 if i miss that how can it worked. – Arif Rahman Jun 28 '18 at 13:54
  • There is insufficient information to go about, post the code on `enqueue` and your error log – HawkPriest Jun 28 '18 at 14:26
  • @HawkPriest enqueue is ok. i figured out it has something to deal with OkHttpClient. The problem is happening for the SSL security. For lower android version retrofit must be configured with OkHttpClient. But i don't know how to do it. – Arif Rahman Jun 28 '18 at 14:34
  • If that's the case then you need to ask your API provider to update the certificates check out this on their issue page https://github.com/square/okhttp/issues/3188 – HawkPriest Jun 28 '18 at 14:36
  • Possible duplicate of [Android pre-lollipop devices giving error "SSL handshake aborted: ssl=0x618d9c18: I/O error during system call, Connection reset by peer"](https://stackoverflow.com/questions/45984295/android-pre-lollipop-devices-giving-error-ssl-handshake-aborted-ssl-0x618d9c18) – Navneet Krishna Jun 28 '18 at 16:27

2 Answers2

0

The problem is not clear without the proper logcat. However, looks like you have SSL certificate issues with your API server. You might consider managing a valid SSL certificate for your API server which will remove the error I think.

As a workaround, you might consider trusting all certificates which are not safe, as described here.

I am copying the code from the tutorial for convenience.

OkHttpClient okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

Retrofit.Builder builder = new Retrofit.Builder()  
        .baseUrl("http://10.0.2.2:3000/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();

UserService userClient = retrofit.create(UserService.class);

Call<ResponseBody> call = userClient.profilePicture("https://revoked.badssl.com/");  
call.enqueue(new Callback<ResponseBody>() {  
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        Toast.makeText(BadSSLActivity.this, "got response" , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Toast.makeText(BadSSLActivity.this, "SSL error?" , Toast.LENGTH_SHORT).show();
    }
});

Please go through the tutorial for better understanding. Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

If you go through the official documentation of Retrofit, it says that "Retrofit requires at minimum Java 8+ or Android API 21+.", where API 21 is the lollipop version. So anything below that will remain unsupported.