java.security.cert.CertPathValidatorException:未找到证书路径的信任锚。在 api 上少于 24

Ami*_*min 8 android android-volley android-security trustmanager x509trustmanager

com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Run Code Online (Sandbox Code Playgroud)

我在 19 到 24 的 api 上的 logcat 中收到此错误,并且我的应用程序中没有从服务器加载数据我搜索了该错误并找到了解决方案

 @SuppressLint("TrulyRandom")
public static void handleSSLHandshake() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }

            @Override
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        }};

        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String arg0, SSLSession arg1) {
                return true;
            }
        });
    } catch (Exception ignored) {
    }
}
Run Code Online (Sandbox Code Playgroud)

并在我的应用程序类 onCreate 中调用它,这解决了我的问题,但在该答案中,如果找到该解决方案,则会提示此代码不相关,不应使用!这是谷歌禁止的。

所以有人知道谷歌针对该错误允许的替代解决方案是什么?