Android Volley错误:“未找到证书路径的信任锚",仅在真实设备中,未在模拟器中 [英] Android volley error: "Trust anchor for certification path not found", only in real device, not emulator

查看:82
本文介绍了Android Volley错误:“未找到证书路径的信任锚",仅在真实设备中,未在模拟器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序出现问题,在我的片段之一中,我使用了截击来发出网络请求:

I'm having a problem in my Android app, in one of my fragments I use volley to do a network request:

JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.POST,
            CustomNetworkManager.getInstance(this.getActivity().getApplicationContext()).getRequestUrl(url),
            requestData,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    // process response
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("FeedFragment", "Volley error: " + error.toString());
                }
            });

在真实设备上,出现以下错误(运行API23):

On a real device I get the following error (running API23):

D/FeedFragment: Volley error: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

在运行相同API版本的AVD中,它工作正常.我检查了其他类似的线程,但找不到答案.

In an AVD running the same API version it is working fine. I checked other similar threads but couldn't find an answer.

感谢您的帮助.

如果任何人都遇到相同的错误,请确保您的证书没有任何问题(

edit: If anyone faces the same error, make sure you don't have any problems with your certificates (http://developer.android.com/intl/pt-br/training/articles/security-ssl.html#CommonProblems)

推荐答案

尝试将此功能添加到您的应用程序中:

try to add this function to your Application:

    /**
     * Enables https connections
     */
    @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) {
        }
    }

,然后在您的应用程序onCreate中调用它.

and then call it in your Application onCreate.

更新:

此代码不相关,不应使用!它被禁止 谷歌.有关更多信息,请此处.

This code is not relevant and shouldn't be used! it is forbidden by Google. for more information look here.

这篇关于Android Volley错误:“未找到证书路径的信任锚",仅在真实设备中,未在模拟器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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