4월, 2017의 게시물 표시

[Android] Retrofit2, OkHttpClient Method 정리

이미지
(이 Post는 Retrofit 2.2.0 기준으로 작성되었음) Retrofit을 적용하면서, 지원되는 기능들을 한번 정리를 해 둬야, 어떤 것을 쓸수 있는지 확실히 할 수 있을 것 같아서, 아래와 같이 정리한다. 1. Retrofit.Builder    baseUrl(String baseUrl) 을 제외하면, 대부분 DI의 형태를 띄고 있다. 각각 Injection 되는 Class들의 용도는 각 항목에서 설명한다. 분석을 하고 보니, Retrofit Class는 이 Builder를 통해 Injection 된 Instance들을 들고있는 Holder 역할을 하는 것으로 보인다. baseUrl(String baseUrl) : Base가 되는 URL을 설정한다. 많은 서비스들에서 사용하는 API들은 앞부분의 Host 정도는 동일하고, 뒷부분의 API Name들로 구분을 하는 경우가 많다. 그때 고정이 되는 앞부분을 여기서는 BaseURL이라고 이야기하고 있다. 만약 BaseUrl을 "https://apis.github.com"이라고 설정한 다음 실제 Retrofit Service Interface에서 @GET("/user/repos")라고 선언해서 사용한다면 실제 요청은 "https://apis.github.com/user/repos"로 나가게 된다. 중요한 부분이 있어서 Retrofit의 Document에서 일부를 가져온다. 올바른 사용: Base URL: http://example.com/api/ Endpoint: foo/bar/ Result: http://example.com/api/foo/bar/ 잘못된 사용: Base URL: http://example.com/api Endpoint: foo/bar/ Result: http://example.com/foo/bar/ 만약 Base URL을 설정하지 않으면 IllegalStateException이 발생하게 된다. baseUrl(HttpU

[Android] @AutoValue 에 대하여.

이미지
An Introduction to AutoValue 라는 글을 보면서 @AutoValue의 사용에 대해 한글로 정리된 글이 별로 없어서 아래와 같이 정리해본다. ( An Introduction to AutoValue 에서 예시들을 가져왔음을 밝힌다) 우선 들어가기 전에 Java에서 유명한 책인 Effective Java의 저자 Joshua Bloch가 AutoValue와 관련해서 아래와 같은 말을 했다고 한다. "AutoValue is a great tool for eliminating the drudgery of writing mundane value classes in Java. It encapsulates much of the advice in Effective Java Chapter 2, and frees you to concentrate on the more interesting aspects of your program. The resulting program is likely to be shorter, clearer, and freer of bugs. Two thumbs up." --  Joshua Bloch, author, Effective Java 위의 말을 발로 번역을 해보자면 "AutoValue는 평범한 Value Class 작성의 힘든 작업들을 줄여주기 위한 훌륭한 툴이다.  Effective Java 2장의 많은 Advice들을 캡슐화해서, 당신의 프로그램에서 흥미로운 부분에 좀 더 집중할 수 있게 해준다. 그 결과로 프로그램은 좀 더 짧아지고, 깨끗하지며, 버그로부터 자유로워 질수 있다. 쌍따봉!!" --  Joshua Bloch, author, Effective Java 은근 책을 홍보하는 내용이 담겨있기는 하지만, AutoValue는 우리 코드 작성을 줄여주는데 도움을 주는 도구이다. 라는 정도만 이해하고 진행하도록 하자. 1. 기본적인 용도 Android를 개발하다보

[Android] Enum대신 @IntDef나 @StringDef를 쓰자.

enum에 대한 최적화가 Android에서는 좀 부족하다는 이야기를 듣고 @IntDef나 @StringDef를 써야 한다 라는 것을 테스트 해봤다. 그것 관련해서 Google의 FlexboxLayout 내에서 사용하는 방식을 알게되어 아래 정리한다. @IntDef ({ FlexDirection . ROW , FlexDirection . ROW_REVERSE , FlexDirection . COLUMN , FlexDirection . COLUMN_REVERSE }) @Retention (RetentionPolicy. SOURCE ) public @ interface FlexDirection { /** * Main axis direction -> horizontal. Main start to * main end -> Left to right (in LTR languages). * Cross start to cross end -> Top to bottom */ int ROW = 0 ; /** * Main axis direction -> horizontal. Main start * to main end -> Right to left (in LTR languages). Cross start to cross end -> * Top to bottom. */ int ROW_REVERSE = 1 ; /** * Main axis direction -> vertical. Main start * to main end -> Top to bottom. Cross start to cross end -> * Left to right (In LTR languages). */ int COLUMN = 2 ; /** * Main axis direction -&g