初めに
今回は、retrofit2
とOkHttp
を使った通信処理の内容をlog catに出す方法を紹介していこうと思います。
すごく単純ではありますが大事な設定なので備忘録として残しておきます。
本文
まず、下記の内容をimportします。
最新のバージョンはこちらのリンクから確認してください。
https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor
implementation("com.squareup.okhttp3:logging-interceptor:4.10.0")
あとはコードで実装するだけです。
.addInterceptor(
HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
}
)
OkHttpClient.Builder
を作成時に下記のコードを挿入するだけです。
最後に
実際によく使われてる機能なので、勉強中の方などのお役に立てれば光栄です。
今回は、log catに出す部分のみを紹介しましたが、下記の自分の記事で全体像を紹介したので、もしわかりづらい人は下記のリンクを参考にしてみてください。
https://qiita.com/ryuji_sato/items/bc7b4daeb30487c4d11e