无论如何,有没有修复android 4.x上的ssl握手错误,SSL库协议错误失败

时间:2019-02-13 13:30:47

标签: java android okhttp

我正在开发和开发Android本机woocomerce应用程序,但遇到的问题是,当我用来生成和生成apk并在android 4.x版上运行时,它不会从api提取数据,而只是显示由协议引起的SSL错误: 附在图片下方 enter image description here 高于4.x的版本(即5或更高)可以正常工作并获取所有数据

我尝试过TLS转换,但未成功

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的连接方式也与其他android版本相同,但我已经检查过SDK是否存在某种我认为的SSL或TLS问题

1 个答案:

答案 0 :(得分:0)

代码不是我写的,但是我在我的项目中得到了它,我可以确认它可以工作。

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扩展不是必需的,但非常有用。