IntelliJ: Retrofit で HTTP リクエスト (data class を使用）

こちらと同じことを行いました。
[kotlinでretrofit2を利用してWebAPIからjson形式データを非同期で取得する方法](http://android.techblog.jp/archives/9614747.html)

次の例と受け取る JSON が異なります。
[IntelliJ: Retrofit で HTTP リクエスト](https://qiita.com/ekzemplaro/items/033c3df046ee7f8e4b0e)
[IntelliJ: Retrofit で HTTP リクエスト (HashMap)](https://qiita.com/ekzemplaro/items/0784fa0e145c77641b2f)

## API の仕様

```text
$ http https://zipcloud.ibsnet.co.jp/api/search?zipcode=1000001
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Length: 287
Content-Type: text/plain;charset=utf-8
Date: Mon, 06 Nov 2023 01:46:20 GMT
Server: Google Frontend
X-Cloud-Trace-Context: b6e49d0061f66fbb2fe79f05a81028cd

{
    "message": null,
    "results": [
        {
            "address1": "東京都",
            "address2": "千代田区",
            "address3": "千代田",
            "kana1": "ﾄｳｷｮｳﾄ",
            "kana2": "ﾁﾖﾀﾞｸ",
            "kana3": "ﾁﾖﾀﾞ",
            "prefcode": "13",
            "zipcode": "1000001"
        }
    ],
    "status": 200
}
```

## 環境

```build.gradele.kts
（省略）
dependencies {
    implementation ("com.squareup.retrofit2:retrofit:2.9.0")
    implementation ("com.squareup.retrofit2:converter-gson:2.9.0")


    testImplementation(kotlin("test"))
（省略）
}
```

## プログラム
ツリー構造
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/ae9b925e-7074-1fa8-baa5-6bc8b5d1c4dd.png)

```Main.kt
import model.ZipApiData
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import service.ZipService
import service.ZipServiceImpl

fun getZipUsingRetrofit(){
    val zipService: ZipService =
        ZipServiceImpl().getZipServiceFactory()

    val call: Call<ZipApiData> = zipService.getZip()

    println("*** check *** aaa ***")
    call.enqueue(object : Callback<ZipApiData> {
        override fun onResponse(call: Call<ZipApiData>,
                                response: Response<ZipApiData>
        ) {
            val dict_aa = (response.body())!!
            println(dict_aa)
            val results = dict_aa.results!!
            println(results)
            val address1 = results[0].address1
            print(address1 + "\t")
            val address2 = results[0].address2
            print(address2 + "\t")
            val address3 = results[0].address3
            println(address3)
        }

        override fun onFailure(call: Call<ZipApiData>, t: Throwable) {
            t.printStackTrace()
        }

    })
}

fun main(){
    println("*** main *** start ***")
    getZipUsingRetrofit()
    println("*** main *** end ***")
}
```

```model/ZipApiData.kt
package model

data class ZipApiData (
    var message: String?,
    var results: List<Results>?,
    var status: Int
)

data class Results(
        var address1: String,
        var address2: String,
        var address3: String,
        var kana1: String,
        var kana2: String,
        var kana3: String,
        var prefcode: String,
        var zipcode: String
)
```

```service/ZipService.kt
package service

import model.ZipApiData
import retrofit2.Call
import retrofit2.http.GET

interface ZipService {
    @GET("/api/search?zipcode=1000001")
    fun getZip(): Call<ZipApiData>
}
```

```service/ZipServiceImpl.kt
package service

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class ZipServiceImpl {

    fun getZipServiceFactory(): ZipService{
        val retrofit = Retrofit.Builder()
            .baseUrl("https://zipcloud.ibsnet.co.jp")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

        return retrofit.create(ZipService::class.java)
    }
}
```

## 実行結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/4b7de898-56ae-ec01-97a2-7b726bd6f15f.png)

```text
*** main *** start ***
*** check *** aaa ***
*** main *** end ***
ZipApiData(message=null, results=[Results(address1=東京都, address2=千代田区, address3=千代田, kana1=ﾄｳｷｮｳﾄ, kana2=ﾁﾖﾀﾞｸ, kana3=ﾁﾖﾀﾞ, prefcode=13, zipcode=1000001)], status=200)
[Results(address1=東京都, address2=千代田区, address3=千代田, kana1=ﾄｳｷｮｳﾄ, kana2=ﾁﾖﾀﾞｸ, kana3=ﾁﾖﾀﾞ, prefcode=13, zipcode=1000001)]
東京都	千代田区	千代田
```