Skip to content Skip to sidebar Skip to footer

Retrofit Is Not Working Below Lollipop

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 co

Solution 1:

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.

OkHttpClientokHttpClient= UnsafeOkHttpClient.getUnsafeOkHttpClient();

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

Retrofitretrofit= builder.build();

UserServiceuserClient= retrofit.create(UserService.class);

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

    @OverridepublicvoidonFailure(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.

Solution 2:

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.

Post a Comment for "Retrofit Is Not Working Below Lollipop"