Android volley error: “Trust anchor for certification path not found”, only in real device, not emulator

前端 未结 3 536
星月不相逢
星月不相逢 2020-12-15 00:11

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(
             


        
相关标签:
3条回答
  • 2020-12-15 00:59

    This solved my problem trying to run my android volley app on arc welder only needs to be run once..at the initial splash activity

    0 讨论(0)
  • 2020-12-15 01:05

    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) {
            }
        }
    

    and then call it in your Application onCreate.

    UPDATE:

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

    0 讨论(0)
  • 2020-12-15 01:13

    Just in case one still uses Volley...

    Follow the instructions here:

    https://developer.android.com/training/articles/security-ssl#java

    Download the certificate file (.crt), put it into your assets directory (next to your java and res directories), then change the following code:

    InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
    

    to use the file from assets:

    InputStream caInput = new BufferedInputStream(getAssets().open("load-der.crt"));
    

    Forget the part after

    // Tell the URLConnection to use a SocketFactory from our SSLContext
    

    and add one single line instead:

    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    

    Run this code before any connections made.

    That's all.

    0 讨论(0)
提交回复
热议问题