2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GsonからMoshiへ(Retrofit2利用)

Last updated at Posted at 2019-11-24

Gsonを使っていたコードをMoshiに変えました。

準備

Gradleの設定

Retrofit2の設定も追加で必要なので忘れずに行うように
(自分はずっと忘れていた。。)

app/build.gradle
// Moshi
dependencies {
    def moshi_version = '1.5.0'
    implementation "com.squareup.moshi:moshi:$moshi_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"

//Retrofit2にも追加で以下が必要 ここを忘れていてずっとできなかった。。。
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
}

比較して書いてみたいと思います。

Gsonの時

Gson.kt

private var retrofit: Retrofit
private val url = "https://qiita.com"

    init {
        var client = httpBuilder.build()
        this.retrofit = Retrofit.Builder()
            .baseUrl(url)//基本のurl設定
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)//カスタマイズしたokhttpのクライアントの設定
            .build()
    }

//Clientを作成
    val httpBuilder: OkHttpClient.Builder get() {
        //httpClinetのBuilderの中に入ってるメソッド使う?
        val httpClient = OkHttpClient.Builder()
        //headerの追加
            httpClient.addInterceptor(Interceptor { chain ->
                val original = chain.request()
                val request = original.newBuilder()
                    .header("Accept", "application/json")
                    .method(original.method(), original.body())
                    .build()

                var response = chain.proceed(request)

                return@Interceptor response
            })
            .readTimeout(30, TimeUnit.SECONDS)

        //log
        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        httpClient.addInterceptor(loggingInterceptor)

        return httpClient
    }

Mohsiの時

Moshi.kt
private var retrofit: Retrofit
private val url = "https://qiita.com"

    init {
        //Moshi
        val moshi: Moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()

        this.retrofit = Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .client(getClient())
            .build()
    }

private fun getClient(): OkHttpClient {
        return OkHttpClient
            .Builder()
            .connectTimeout(120, TimeUnit.SECONDS)
            .readTimeout(120, TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            })
            .build()
    }

まとめ

見た目がわかりやすくなった。

Retrofit2とOkHttp3については過去記事をご覧ください。
Retrofit2とOkhttp3を使ったAPI開発

2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?