Retrofit 2.0-beta で ログを吐かせる

Retrofit

Okhttp 2.6.0+ では HTTPリクエスト/レスポンスのログ出力が可能なのだが,

New Logging Interceptor. The logging-interceptor subproject offers simple request and response logging. It may be configured to log headers and bodies for debugging. It requires this Maven dependency:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(Level.BASIC);
client.networkInterceptors().add(logging);

okhttp/CHANGELOG.md at master · square/okhttp

残念なことに retrofit 2.0.0-beta2 での同梱は 2.5.0 .

<!-- Dependencies -->
<android.version>4.1.1.4</android.version>
<android.platform>16</android.platform>
<okhttp.version>2.5.0</okhttp.version>
<animal.sniffer.version>1.14</animal.sniffer.version>

retrofit/pom.xml at master · square/retrofit

なので logging-interceptor 2.6.0 で.

    <dependency>
      <groupId>com.squareup.okhttp</groupId>
      <artifactId>okhttp</artifactId>
      <version>${project.version}</version>
    </dependency>

okhttp/okhttp-logging-interceptor at master · square/okhttp

compile 'com.squareup.okhttp:logging-interceptor:2.6.0' 
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
logging.setLevel(Level.BASIC);
OkHttpClient client = new OkHttpClient();  
httpClient.interceptors().add(logging);
Retrofit retrofit = new Retrofit.Builder()  
   .baseUrl(API_BASE_URL)
   .addConverterFactory(GsonConverterFactory.create())
   .client(httpClient)
   .build();

出力.

...
11:33:04.547 17090-17108 D/OkHttp: <-- HTTP/1.1 200 OK (469ms, 259-byte body)
11:33:04.550 17090-17296 D/OkHttp: --> GET /v5/item/1072964 HTTP/1.1
11:33:04.563 17090-17263 D/OkHttp: <-- HTTP/1.1 200 OK (250ms, 244-byte body)
11:33:04.566 17090-17108 D/OkHttp: --> GET /v5/item/1073510 HTTP/1.1
11:33:04.606 17090-17265 D/OkHttp: <-- HTTP/1.1 200 OK (252ms, 228-byte body)
11:33:04.609 17090-17263 D/OkHttp: --> GET /v5/item/1072585 HTTP/1.1
11:33:04.794 17090-17296 D/OkHttp: <-- HTTP/1.1 200 OK (244ms, 183-byte body)
11:33:04.796 17090-17265 D/OkHttp: --> GET /v5/item/1073275 HTTP/1.1

stable で対応するのかな.



android-apt で アノテーションを便利に

このプラグインを追加すると、gradle のソースフォルダとして認識される場所に Butter Knife で生成されるコードが格納されます。
そのため、Butter Knife で生成されたクラスも Lint から見えるようになり、Lint が怒らなくなります。やったー。

Y.A.M の 雑記帳: Butter Knife のフィールドやメソッドが Lint に Unused declaration と言われないようにする方法

JakeWharton commented on Sep 26
If you use the android-apt plugin then Android Studio can see the generated code and the method will not show up as unused.

Lint: Unused resource · Issue #356 · JakeWharton/butterknife

Bitbucket で公開されています.

The android-apt plugin assists in working with annotation processors in combination with Android Studio. It has two purposes:
Allow to configure a compile time only annotation processor as a dependency, not including the artifact in the final APK or library
Set up the source paths so that code that is generated from the annotation processor is correctly picked up by Android Studio.

hvisser / android-apt — Bitbucket

「APT」て何?

Annotation Processing Boilerplate Destruction (Droidcon NYC 2014) // Speaker Deck

Butter Knife 以外にも使えるのか.

わーい.


Android OS バージョンのコードネームを取得する

端末のOSのバージョンをみる.


Log.d(TAG, "RELEASE : " + Build.VERSION.RELEASE);
Log.d(TAG, "SDK     : " + Build.VERSION.SDK_INT);


RELEASE : 4.3.1
SDK     : 18

「API-18」て「Gingerbread」だったけか?

などといつもググっているが.

Dashboards___Android_Developers

Dashboards | Android Developers

いい暗記方法ねえかなと思いつつコードで吐く.


for (Field field : Build.VERSION_CODES.class.getDeclaredFields()) {
  field.setAccessible(true);
  Log.d(TAG, field.get(this) + " -> " + field.getName());
}


1 -> BASE
2 -> BASE_1_1
3 -> CUPCAKE
10000 -> CUR_DEVELOPMENT
4 -> DONUT
5 -> ECLAIR
6 -> ECLAIR_0_1
7 -> ECLAIR_MR1
8 -> FROYO
9 -> GINGERBREAD
10 -> GINGERBREAD_MR1
11 -> HONEYCOMB
12 -> HONEYCOMB_MR1
13 -> HONEYCOMB_MR2
14 -> ICE_CREAM_SANDWICH
15 -> ICE_CREAM_SANDWICH_MR1
16 -> JELLY_BEAN
17 -> JELLY_BEAN_MR1
18 -> JELLY_BEAN_MR2

他の端末で.


RELEASE : 5.1.1
SDK     : 22

1 ->BASE
2 -> BASE_1_1
3 -> CUPCAKE
10000 -> CUR_DEVELOPMENT
4 -> DONUT
5 -> ECLAIR
6 -> ECLAIR_0_1
7 -> ECLAIR_MR1
8 -> FROYO
9 -> GINGERBREAD
10 -> GINGERBREAD_MR1
11 -> HONEYCOMB
12 -> HONEYCOMB_MR1
13 -> HONEYCOMB_MR2
14 -> ICE_CREAM_SANDWICH
15 -> ICE_CREAM_SANDWICH_MR1
16 -> JELLY_BEAN
17 -> JELLY_BEAN_MR1
18 -> JELLY_BEAN_MR2
19 -> KITKAT
20 -> KITKAT_WATCH
21 -> L
21 -> LOLLIPOP
22 -> LOLLIPOP_MR1

その端末が知っているOSコードネーム群の最終がその端末のOSコードネームと想像.


Field[] fields = Build.VERSION_CODES.class.getDeclaredFields();
Log.d(TAG, "Name : " + fields[fields.length - 1].getName());


Name : LOLLIPOP_MR1

最終でないことはあり得るのか.

フツーに走査か.

Cross Reference: Build.java

追記:2020-03-13

Kotlin で。

追記:2022-01-26