もうこれはテンプレ化しておきます。
Retrofit の話が中心となります。
👉 Retrofit ![](http://b.hatena.ne.jp/entry/image/https://square.github.io/retrofit/)
Converter
![Retrofit-Converters](https://i0.wp.com/android.benigumo.com/wp-content/uploads/2022/03/f2b9c432888aefc7c4780b79da40a124.png?ssl=1)
JSON 形式のレスポンスをパースして変換するのは、Gson か Moshi が人気のように思います。
var retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com")
.addConverterFactory(GsonConverterFactory.create())
.build()
var retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com")
.addConverterFactory(MoshiConverterFactory.create())
.build()
👉 retrofit/retrofit-converters at master · square/retrofit ![](http://b.hatena.ne.jp/entry/image/https://github.com/square/retrofit/tree/master/retrofit-converters)
今回は、Kotlin Serialization を利用した「Kotlin Serialization Converter」を使います。
ExperimentalSerializationApi なのですがね。
val contentType = "application/json".toMediaType()
val retrofit = Retrofit.Builder()
.baseUrl("https://example.com/")
.addConverterFactory(Json.asConverterFactory(contentType))
.build()
👉 JakeWharton/retrofit2-kotlinx-serialization-converter: A Retrofit 2 Converter.Factory for Kotlin serialization. ![](http://b.hatena.ne.jp/entry/image/https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter)
Call Adapter
![Retrofit CallAdapters](https://i0.wp.com/android.benigumo.com/wp-content/uploads/2022/03/d6d1099473cc4824c17903fa946f8889.png?ssl=1)
通信の非同期処理はどれに任せるか。
var retrofit = Retrofit.Builder()
.baseUrl("https://api.example.com")
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build()
👉 retrofit/retrofit-adapters at master · square/retrofit ![](http://b.hatena.ne.jp/entry/image/https://github.com/square/retrofit/tree/master/retrofit-adapters)
Coroutine を使った Jake製の「Kotlin Coroutine Adapter」は 今では DEPRECATED。
👉 JakeWharton/retrofit2-kotlin-coroutines-adapter: A Retrofit 2 adapter for Kotlin coroutine's Deferred type. ![](http://b.hatena.ne.jp/entry/image/https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter)
Retrofit は 2.6.0+ で、内部に suspend function をサポートしてるので、特に、.addCallAdapterFactory()
を使わなくてもよい。
@GET("users/{id}")
suspend fun user(@Path("id") id: Long): User
👉 retrofit/CHANGELOG.md at master · square/retrofit ![](http://b.hatena.ne.jp/entry/image/https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-260-2019-06-05)
HttpLoggingInterceptor
OkHttpClient に HTTP関連のログを吐かせます。
--> POST /greeting http/1.1
Host: example.com
Content-Type: plain/text
Content-Length: 3
Hi?
--> END POST
<-- 200 OK (22ms)
Content-Type: plain/text
Content-Length: 6
Hello!
<-- END HTTP
👉 HttpLoggingInterceptor.Level (OkHttp Logging Interceptor 3.14.0 API) ![](http://b.hatena.ne.jp/entry/image/https://square.github.io/okhttp/3.x/logging-interceptor/okhttp3/logging/HttpLoggingInterceptor.Level.html)
val logging = HttpLoggingInterceptor()
logging.setLevel(Level.BASIC)
val client = OkHttpClient.Builder()
.addInterceptor(logging)
.build()
👉 okhttp/okhttp-logging-interceptor at master · square/okhttp ![](http://b.hatena.ne.jp/entry/image/https://github.com/square/okhttp/tree/master/okhttp-logging-interceptor)
まとめ
Dagger の Module にしておきます。
テンプレート化してください、と言わんばかりに
よく似たものをあちこちで見かけますよね。
見通しも良くなります。
コピペでどうぞ。
(おわり)