改造工作不低于棒棒糖(Retrofit is not working below Lollipop)

2019-10-29 09:25发布

我试图让使用从我的网站数据Retrofit 。 它的正常工作在Android 5.0,但较小的Android版本出现错误消息Connection closed by peer 。 这里是我的代码...

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

这里是MyService

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

我缺少的是在这里吗? 它是完全工作正常了的是Android 5.0。 我认为这是值得处理SSL握手和OkHttpClient 。 我不知道如何实现OkHttpClientRetrofit

这里是我的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

Answer 1:

问题是没有适当的logcat的清晰。 但是,看起来你有你的API服务器的SSL证书的问题。 你可能会考虑为管理您的API服务器的有效SSL证书,这将删除我认为错误。

作为一种解决办法,因为你可能会考虑信任它是不是安全的所有证书, 在这里描述 。

我复制本教程为了方便代码。

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();
    }
});

请通过教程更好的理解。 希望帮助。



文章来源: Retrofit is not working below Lollipop