title: Https is showing unexpected behavior in Android with certificates
body: <p>I am using HTTPS and some of the APIs are running fine and some give the following exception. In some devices i.e. S4,Lenovo having 4.4.x or above some services are working fine but on S3 with 4.3 version I'm unable to even login in app. I've two certificate files but ignored one of them (intermediate file). Kindly guide towards the solution. 
  Thanks</p>
  
  <p>Here is the code:</p>
  
  <pre><code>String serverUrl = Constants.BASE_URL + mAction;
          if (mParams != null) {
              serverUrl += "?" + mParams;
          }
          Log.d("usm_serverUrl",serverUrl);
          try {
  
              //-----------------------------------------------------------------------
  
              // Load CAs from an InputStream
  // (could be from a resource or ByteArrayInputStream or ...)
              CertificateFactory cf = CertificateFactory.getInstance("X.509");
  
              InputStream caInput  = ctx.getResources().openRawResource(R.raw.route2school_be);
              Certificate ca;
              try {
                  ca = cf.generateCertificate(caInput);
                  System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
                  Log.d("usm_ca","ca=" + ((X509Certificate) ca).getSubjectDN());
              } finally {
                  caInput.close();
              }
  
  // Create a KeyStore containing our trusted CAs
              String keyStoreType = KeyStore.getDefaultType();
              KeyStore keyStore = KeyStore.getInstance(keyStoreType);
              keyStore.load(null, null);
              keyStore.setCertificateEntry("ca", ca);
  
  // Create a TrustManager that trusts the CAs in our KeyStore
              String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
              TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
              tmf.init(keyStore);
  
  // Create an SSLContext that uses our TrustManager
              SSLContext context = SSLContext.getInstance("TLS");
              context.init(null, tmf.getTrustManagers(), null);
  
              //--------------------------------------------------
  
              URL url = new URL(serverUrl);
  
  
              HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
              conn.setSSLSocketFactory(context.getSocketFactory());
  
                conn.setRequestProperty("User-Agent", "application/json");
                conn.setRequestMethod("GET");
              InputStream in = new BufferedInputStream(conn.getInputStream());
              String response = IOUtils.toString(in, "UTF-8");
              Log.d("debug", "get-url: " + serverUrl + "\n post-response: " + response);
              return response;
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
  </code></pre>
  
  <p>Exception:</p>
  
  <pre><code>   java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                                                           javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                                                               at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:322)
                                                                               at com.android.okhttp.Connection.upgradeToTls(Connection.java:1257)
                                                                               at com.android.okhttp.Connection.connect(Connection.java:1188)
                                                                               at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:395)
                                                                               at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298)
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399)
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349)
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517)
                                                                               at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
                                                                               at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
                                                                               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:115)
                                                                               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:86)
                                                                               at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:93)
                                                                               at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:73)
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:264)
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237)
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135)
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                               at java.lang.Thread.run(Thread.java:818)
                                                                            Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                                                               at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:318)
                                                                               at com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:219)
                                                                               at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:114)
                                                                               at com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:550)
                                                                               at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
                                                                               at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
                                                                               at com.android.okhttp.Connection.upgradeToTls(Connection.java:1257) 
                                                                               at com.android.okhttp.Connection.connect(Connection.java:1188) 
                                                                               at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:395) 
                                                                               at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298) 
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399) 
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349) 
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517) 
                                                                               at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) 
                                                                               at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25) 
                                                                               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:115) 
                                                                               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:86) 
                                                                               at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:93) 
                                                                               at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:73) 
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:264) 
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237) 
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135) 
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                               at java.lang.Thread.run(Thread.java:818) 
                                                                            Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                                                               at com.android.org.conscrypt.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:318) 
                                                                               at com.android.org.conscrypt.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:219) 
                                                                               at com.android.org.conscrypt.Platform.checkServerTrusted(Platform.java:114) 
                                                                               at com.android.org.conscrypt.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:550) 
                                                                               at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method) 
                                                                               at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318) 
                                                                               at com.android.okhttp.Connection.upgradeToTls(Connection.java:1257) 
                                                                               at com.android.okhttp.Connection.connect(Connection.java:1188) 
                                                                               at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:395) 
                                                                               at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:298) 
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:399) 
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:349) 
                                                                               at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:517) 
                                                                               at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) 
                                                                               at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25) 
                                                                               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStreamFromNetwork(BaseImageDownloader.java:115) 
                                                                               at com.nostra13.universalimageloader.core.download.BaseImageDownloader.getStream(BaseImageDownloader.java:86) 
                                                                               at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.getImageStream(BaseImageDecoder.java:93) 
                                                                               at com.nostra13.universalimageloader.core.decode.BaseImageDecoder.decode(BaseImageDecoder.java:73) 
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.decodeImage(LoadAndDisplayImageTask.java:264) 
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:237) 
                                                                               at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:135) 
                                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                               at java.lang.Thread.run(Thread.java:818) 
  </code></pre>