1
3

More than 1 year has passed since last update.

通信内容をlog catに出したい

Posted at

初めに

今回は、retrofit2OkHttpを使った通信処理の内容を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

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3