This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| retrofit [2021/02/07 03:50] – removed ledyx | retrofit [2021/02/07 05:49] (current) – old revision restored (2017/06/22 14:06) ledyx | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | = Retrofit = | ||
| + | http:// | ||
| + | |||
| + | Retrofit2 + OkHttp3 + Jackson + RxAndroid2/ | ||
| + | |||
| + | {{tag> | ||
| + | |||
| + | |||
| + | = Library = | ||
| + | |||
| + | https:// | ||
| + | https:// | ||
| + | |||
| + | |||
| + | <sxh bash ; title: | ||
| + | compile ' | ||
| + | compile ' | ||
| + | compile ' | ||
| + | |||
| + | compile ' | ||
| + | compile ' | ||
| + | compile ' | ||
| + | |||
| + | compile ' | ||
| + | compile ' | ||
| + | compile ' | ||
| + | |||
| + | //Android | ||
| + | compile ' | ||
| + | //Java | ||
| + | compile ' | ||
| + | </ | ||
| + | |||
| + | = Source = | ||
| + | |||
| + | <sxh java ; title: | ||
| + | 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 volatile ConcurrentHashMap< | ||
| + | |||
| + | public static synchronized <T> T getInstance(Class< | ||
| + | if(!restfulInterface.isInterface()) | ||
| + | throw new IllegalArgumentException(" | ||
| + | |||
| + | T service = (T) services.get(restfulInterface.getName()); | ||
| + | |||
| + | if(service == null) { | ||
| + | if(!restfulInterface.isAnnotationPresent(URL.class)) | ||
| + | throw new Exception(" | ||
| + | |||
| + | URL urlAnnotation = restfulInterface.getDeclaredAnnotation(URL.class); | ||
| + | |||
| + | String url = urlAnnotation.value(); | ||
| + | if(url.length() <= 0 || !url.substring(0, | ||
| + | throw new Exception(" | ||
| + | |||
| + | boolean isUnwrapRootValue = urlAnnotation.isUnwrappedRoot(); | ||
| + | |||
| + | HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); | ||
| + | httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); | ||
| + | |||
| + | CookieManager cookieManager = new CookieManager(); | ||
| + | cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); | ||
| + | |||
| + | OkHttpClient client = configureClient() // 인증서 무시 | ||
| + | .connectTimeout(CONNECT_TIMEOUT, | ||
| + | .writeTimeout(WRITE_TIMEOUT, | ||
| + | .readTimeout(READ_TIMEOUT, | ||
| + | .cookieJar(new JavaNetCookieJar(cookieManager)) //쿠키 설정 | ||
| + | .addInterceptor(httpLoggingInterceptor) //http 로그 확인 | ||
| + | .build(); | ||
| + | |||
| + | service = new Retrofit.Builder() | ||
| + | .baseUrl(url) | ||
| + | .client(client) | ||
| + | .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||
| + | .addConverterFactory(getConverterFactory(isUnwrapRootValue)) | ||
| + | .build().create(restfulInterface); | ||
| + | |||
| + | services.put(restfulInterface.getName(), | ||
| + | } | ||
| + | |||
| + | return service; | ||
| + | } | ||
| + | |||
| + | private static JacksonConverterFactory getConverterFactory(boolean isUnwrapRootValue) { | ||
| + | ObjectMapper objectMapper = new ObjectMapper(); | ||
| + | objectMapper.registerModule(new JodaModule()); | ||
| + | objectMapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES); | ||
| + | objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
| + | if(isUnwrapRootValue) | ||
| + | objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); | ||
| + | | ||
| + | return JacksonConverterFactory.create(objectMapper); | ||
| + | } | ||
| + | | ||
| + | /** | ||
| + | * 인증서 무시 | ||
| + | * @return | ||
| + | */ | ||
| + | private static OkHttpClient.Builder configureClient() throws Exception { | ||
| + | TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| + | trustManagerFactory.init((KeyStore) null); | ||
| + | TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); | ||
| + | |||
| + | if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) | ||
| + | throw new IllegalStateException(" | ||
| + | |||
| + | X509TrustManager trustManager = (X509TrustManager) trustManagers[0]; | ||
| + | |||
| + | SSLContext sslContext = SSLContext.getInstance(" | ||
| + | sslContext.init(null, | ||
| + | |||
| + | OkHttpClient.Builder builder = new OkHttpClient.Builder(); | ||
| + | builder.sslSocketFactory(sslContext.getSocketFactory(), | ||
| + | |||
| + | return builder; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: | ||
| + | @URL(value = " | ||
| + | public interface DemoService { | ||
| + | @GET(" | ||
| + | Observable< | ||
| + | |||
| + | @PUT(" | ||
| + | Observable< | ||
| + | |||
| + | @Multipart | ||
| + | @POST(URL) | ||
| + | Observable< | ||
| + | |||
| + | @DELETE(" | ||
| + | Observable< | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <sxh java ; title: | ||
| + | @Target(ElementType.TYPE) | ||
| + | @Retention(RetentionPolicy.RUNTIME) | ||
| + | public @interface URL { | ||
| + | String value() default ""; | ||
| + | boolean isUnwrappedRoot() default false; | ||
| + | } | ||
| + | </ | ||