首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否在Android4.x上修复ssl握手错误,SSL库协议错误

是否在Android4.x上修复ssl握手错误,SSL库协议错误
EN

Stack Overflow用户
提问于 2019-02-13 13:30:46
回答 1查看 589关注 0票数 0

我正在开发android本机woocomerce应用程序,我所面临的问题是,当我在Android4.x版本上构建和生成apk并运行时,它不会从api中获取数据,只是显示了协议引起的一个SSL错误:附在下面的 在这里输入图像描述版本4.xi.e 5之上,或者在工作中很好地获取所有数据。

我试过TLS转换,但没有完全成功。

代码语言:javascript
复制
package com.app.androidwoocommerce.network;


import com.app.androidwoocommerce.constant.ConstantValues;
import com.app.androidwoocommerce.oauth.BasicOAuth;
import com.app.androidwoocommerce.oauth.OAuthInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * APIClient handles all the Network API Requests using Retrofit Library
 **/

public class APIClient {

    public static Retrofit retrofit;
    private static APIRequests apiRequests;
    private static final String BASE_URL = ConstantValues.WOOCOMMERCE_URL;


    // Singleton Instance of APIRequests
    public static APIRequests getInstance() {
        if (apiRequests == null) {

            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

            OAuthInterceptor oauth1Woocommerce = new OAuthInterceptor.Builder()
                    .consumerKey(ConstantValues.WOOCOMMERCE_CONSUMER_KEY)
                    .consumerSecret(ConstantValues.WOOCOMMERCE_CONSUMER_SECRET)
                    .build();

            BasicOAuth basicOAuthWoocommerce = new BasicOAuth.Builder()
                    .consumerKey(ConstantValues.WOOCOMMERCE_CONSUMER_KEY)
                    .consumerSecret(ConstantValues.WOOCOMMERCE_CONSUMER_SECRET)
                    .build();

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .addInterceptor(BASE_URL.startsWith("http://")?  oauth1Woocommerce : basicOAuthWoocommerce)
                    .build();
           /* OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                    .connectTimeout(30, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .writeTimeout(30, TimeUnit.SECONDS)
                    .addInterceptor(BASE_URL.startsWith("http://")?  oauth1Woocommerce : basicOAuthWoocommerce)
                    .addInterceptor(interceptor)
                    .build();*/


            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();


            apiRequests = retrofit.create(APIRequests.class);

            return apiRequests;

        }
        else {
            return apiRequests;
        }
    }

}

我希望API连接到设备4.x,在其他版本中也是如此,min已经被检查过了,它有某种ssl或tls问题,我认为

EN

回答 1

Stack Overflow用户

发布于 2019-02-13 13:34:37

代码不是由我编写的,但是我在我的项目中得到了它,我可以确认它是有效的。

代码语言:javascript
复制
fun Context.installTls12() {
    // important code starts here
    try {
        ProviderInstaller.installIfNeeded(this)
    } catch (e: GooglePlayServicesRepairableException) {
        // Prompt the user to install/update/enable Google Play services.
        GoogleApiAvailability.getInstance().showErrorNotification(this, e.connectionStatusCode)
    } catch (e: GooglePlayServicesNotAvailableException) {
        // Indicates a non-recoverable error: let the user know.
    }
}

Java版本应该看起来非常类似。早点打电话。Kotlin扩展并不是必需的,但是它非常有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54671425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档