Created
April 4, 2017 18:35
-
-
Save cfirmo33/0c1ca8312a0b6d338c844aaa4863b092 to your computer and use it in GitHub Desktop.
RetrofitUtils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RetrofitUtils { | |
private static Retrofit retrofit; | |
private static OkHttpClient getUnsafeOkHttpClient() { | |
try { | |
final TrustManager[] trustAllCerts = new TrustManager[]{ | |
new X509TrustManager() { | |
@Override | |
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
@Override | |
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException { | |
} | |
@Override | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return new java.security.cert.X509Certificate[]{}; | |
} | |
} | |
}; | |
final SSLContext sslContext = SSLContext.getInstance("SSL"); | |
sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); | |
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); | |
OkHttpClient.Builder builder = new OkHttpClient.Builder(); | |
builder.sslSocketFactory(sslSocketFactory); | |
builder.hostnameVerifier((hostname, session) -> true); | |
OkHttpClient okHttpClient = builder.build(); | |
return okHttpClient; | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static Retrofit getRetrofitClient(){ | |
if(retrofit == null){ | |
String ip = UnimobileApplication.getInstance().getConfiguracao().getIp(); | |
String baseUrl = ("https://" + ip + ":8443/"); | |
retrofit = new Retrofit.Builder() | |
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create()) | |
.addConverterFactory(JacksonConverterFactory.create()) | |
.baseUrl(baseUrl) | |
.client(getUnsafeOkHttpClient()) | |
.build(); | |
} | |
return retrofit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment