问题
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
回答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.
来源:https://stackoverflow.com/questions/51084091/retrofit-is-not-working-below-lollipop