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.
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.
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"