Skip to content

Instantly share code, notes, and snippets.

@leeyc09
Last active June 2, 2017 07:36
Show Gist options
  • Save leeyc09/4f91035ca0bfddb59f74 to your computer and use it in GitHub Desktop.
Save leeyc09/4f91035ca0bfddb59f74 to your computer and use it in GitHub Desktop.
public class RestfulAdapter {
public static final int CONNECT_TIMEOUT = 15;
public static final int WRITE_TIMEOUT = 15;
public static final int READ_TIMEOUT = 15;
private static final String SERVER_URL = "https://api.server.net/"; //2부터 url뒤에 /를 입력해야 합니다.
private static OkHttpClient client;
private static sample_Interface Interface;
public synchronized static sample_Interface getInstance() {
if (Interface == null) {
//통신로그를 확인하기 위한 부분
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//쿠키 메니저의 cookie policy를 변경 합니다.
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
//OkHttpClient를 생성합니다.
client = configureClient(new OkHttpClient().newBuilder()) //인증서 무시
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS) //연결 타임아웃 시간 설정
.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS) //쓰기 타임아웃 시간 설정
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) //읽기 타임아웃 시간 설정
.cookieJar(new JavaNetCookieJar(cookieManager)) //쿠키메니져 설정
.addInterceptor(httpLoggingInterceptor) //http 로그 확인
.build();
//Retrofit 설정
Interface = new Retrofit.Builder()
.baseUrl(DIRECTFOLDER_SERVER)
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create()) //Rxandroid를 사용하기 위해 추가(옵션)
.addConverterFactory(GsonConverterFactory.create()) //Json Parser 추가
.build().create(samaple_Interface.class); //인터페이스 연결
}
return Interface;
}
/**
* UnCertificated 허용
*/
public static OkHttpClient.Builder configureClient(final OkHttpClient.Builder builder) {
final TrustManager[] certs = new TrustManager[]{new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkServerTrusted(final X509Certificate[] chain,
final String authType) {
}
@Override
public void checkClientTrusted(final X509Certificate[] chain,
final String authType) {
}
}};
SSLContext ctx = null;
try {
ctx = SSLContext.getInstance("TLS");
ctx.init(null, certs, new SecureRandom());
} catch (final java.security.GeneralSecurityException ex) {
ex.printStackTrace();
}
try {
final HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(final String hostname, final SSLSession session) {
return true;
}
};
builder.sslSocketFactory(ctx.getSocketFactory()).hostnameVerifier(hostnameVerifier);
} catch (final Exception e) {
e.printStackTrace();
}
return builder;
}
}
@LenKIM
Copy link

LenKIM commented Jun 2, 2017

혹시 Import 상황도 포함시켜주실수 있을까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment