diff --git a/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java b/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java index 5ce81d0..892a4ab 100644 --- a/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java +++ b/app/src/main/java/org/houxg/leamonax/network/ApiProvider.java @@ -11,6 +11,14 @@ import org.houxg.leamonax.network.api.UserApi; import java.io.IOException; +import java.util.concurrent.TimeUnit; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import okhttp3.HttpUrl; import okhttp3.Interceptor; @@ -70,6 +78,37 @@ public void log(String message) { builder.addNetworkInterceptor(interceptor); builder.addNetworkInterceptor(new StethoInterceptor()); } + final TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) { + } + + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) { + } + + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return new java.security.cert.X509Certificate[]{}; + } + } + }; + try { + final SSLContext sslContext = SSLContext.getInstance("SSL"); + sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); + final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); + builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]); + builder.hostnameVerifier(new HostnameVerifier() { + @Override + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + } catch (Exception e) { + e.printStackTrace(); + } + builder.connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS); OkHttpClient client = builder.build(); mApiRetrofit = new Retrofit.Builder() .baseUrl(host + "/api/")