棒棒糖以下无法进行改装 [英] Retrofit is not working below Lollipop

查看:109
本文介绍了棒棒糖以下无法进行改装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Retrofit从我的网站获取数据.从Android 5.0可以正常工作,但显示错误消息Connection closed by peer的Android版本较低.这是我的代码...

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

这是MyService

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

我在这里想念什么?在Android 5.0上完全可以正常工作.我认为这是处理SSL握手和OkHttpClient的东西.我不知道如何用Retrofit实现OkHttpClient.

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.

这是我的logcat

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

推荐答案

如果没有适当的logcat,问题将无法解决.但是,您的API服务器似乎有SSL证书问题.您可能会考虑为您的API服务器管理有效的SSL证书,这会消除我认为的错误.

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.

这篇关于棒棒糖以下无法进行改装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆