title: Exoplayer. How to validate server certificate?
body: <p>I am trying to connect to a server through https and Android does not recognize the certificate as "trusted", so I get the following exception:            </p>
  
  <pre><code>javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:
  Trust anchor for certification path not found.
  </code></pre>
  
  <p>To solve this problem, I have Included the certificate in the app (assets folder) and use it to validate the server certificate.</p>
  
  <p>I do it with a class called CustomSSLSocketFactory where it is read the certificate and make the validation process during the https connection without problem.</p>
  
  <p><a href="https://www.programacion.com.py/moviles/android/https-y-ssl-en-android" rel="nofollow noreferrer">link in Spanish</a></p>
  
  <p><a href="https://translate.google.com/translate?sl=es&amp;tl=en&amp;js=y&amp;prev=_t&amp;hl=es&amp;ie=UTF-8&amp;u=https%3A%2F%2Fwww.programacion.com.py%2Fmoviles%2Fandroid%2Fhttps-y-ssl-en-android&amp;edit-text=&amp;act=url" rel="nofollow noreferrer">link in English</a></p>
  
  <pre><code>HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  conn.setSSLSocketFactory(CustomSSLSocketFactory.getSSLSocketFactory(context));
  </code></pre>
  
  <p>Now I have to repeat this process but using exoplayer2 to play the hlsvideo, and after several days investigating I do not achieve it.</p>
  
  <pre><code>private MediaSource buildMediaSourceHlsMedia(Uri uri) throws MalformedURLException {
  
      String UserAgent = Util.getUserAgent(getContext (), getResources().getString(R.string.app_name));
      int minLoadableRetryCount;
      DefaultHttpDataSourceFactory dataSourceFactory =new DefaultHttpDataSourceFactory( UserAgent,BANDWIDTH_METER,DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS,true);
      dataSourceConHeader(dataSourceFactory,uri.toString(),mParam4);
      MediaSource ms= new HlsMediaSource.Factory(dataSourceFactory).setMinLoadableRetryCount(5).createMediaSource(uri);
      return ms;
  
  }
  </code></pre>
  
  <p>I suppose that it is necessary to code a custom datasource but after several days, I can not achieve it. I do not know how to do it.
  I suppose that I have to include in the custom datasource , (with the rest of necessary code) :<br>
                         <code>conn.setSSLSocketFactory(CustomSSLSocketFactory.getSSLSocketFactory(context));</code></p>
  
  <p>Can someone guide me?. Thanks</p>