IntelliJ: Retrofit で HTTP リクエスト (HashMap)

こちらのプログラムを改造して HashMap の JSON を受け取るようにしました。
[IntelliJ: Retrofit で HTTP リクエスト](https://qiita.com/ekzemplaro/items/033c3df046ee7f8e4b0e)

## 受信する JSON

```json:cities.json
{
  "t0921": { "name": "宇都宮", "population": 41295, "date_mod": "2003-8-12" },
  "t0922": { "name": "小山", "population": 38756, "date_mod": "2003-5-15" },
  "t0923": { "name": "佐野", "population": 71294, "date_mod": "2003-6-8" },
  "t0924": { "name": "足利", "population": 27138, "date_mod": "2003-7-21" },
  "t0925": { "name": "日光", "population": 74682, "date_mod": "2003-4-19" },
  "t0926": { "name": "下野", "population": 82951, "date_mod": "2003-10-14" },
  "t0927": { "name": "さくら", "population": 96823, "date_mod": "2003-5-24" },
  "t0928": { "name": "矢板", "population": 57926, "date_mod": "2003-2-12" },
  "t0929": { "name": "真岡", "population": 64187, "date_mod": "2003-11-14" },
  "t0930": { "name": "栃木", "population": 82354, "date_mod": "2003-7-04" },
  "t0931": { "name": "大田原", "population": 72681, "date_mod": "2003-9-17" },
  "t0932": { "name": "鹿沼", "population": 23749, "date_mod": "2003-7-20" },
  "t0933": { "name": "那須塩原", "population": 12759, "date_mod": "2003-3-12" },
  "t0934": { "name": "那須烏山", "population": 62531, "date_mod": "2003-8-17" }
}
```

## 環境

```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/e9a64fd4-e351-9951-9ca1-b721a049d6b2.png)


```Main.kt
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import service.CityService
import service.CityServiceImpl

fun getAllCitiesUsingRetrofit(){
    val cityService: CityService =
        CityServiceImpl().getCityServiceFactory()

    val call: Call<HashMap <String, HashMap <String,String>>> = cityService.getAllCities()

    call.enqueue(object : Callback<HashMap <String, HashMap <String,String>>> {
        override fun onResponse(call: Call<HashMap <String, HashMap <String,String>>>,
                  response: Response<HashMap <String, HashMap <String,String>>>
        ) {
            //      response.body()?.forEach(::println)

            val dict_aa = (response.body())!!

            println()
            println(dict_aa::class.qualifiedName)
val str_out_aa = dict_to_str_proc (dict_aa,"\t")
            println(str_out_aa)

        }

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

        }

    })
}

fun dict_to_str_proc
	(dict_aa: HashMap <String, HashMap <String,String>>,delim:String)
		: String
{
//	val keys =  dict_aa.keySet ()
	val keys =  dict_aa.keys

	var str_out = ""

	keys.forEach {
		val key=it
		val unit_aa = dict_aa[key]
		str_out += key
		str_out += delim
		str_out += unit_aa?.get("name")
		str_out += delim
		str_out += unit_aa?.get("population")
		str_out += delim
		str_out += unit_aa?.get("date_mod")
		str_out += "\n"
		}

	return	str_out
}



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

```service/Cityservice.kt
package service

import retrofit2.Call
import retrofit2.http.GET

interface CityService {
    @GET("/tmp/cities.json")
    fun getAllCities(): Call<HashMap <String, HashMap <String,String>>>
}
```

```service/CityserviceImpl.kt
package service

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

class CityServiceImpl {
    fun getCityServiceFactory(): CityService{
        val retrofit = Retrofit.Builder()
            .baseUrl("https://ekzemplaro.org")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

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

## 実行結果

```text
*** start ***
*** end ***

java.util.HashMap
t0931	大田原	72681	2003-9-17
t0930	栃木	82354	2003-7-04
t0922	小山	38756	2003-5-15
t0933	那須塩原	12759	2003-3-12
t0921	宇都宮	41295	2003-8-12
t0932	鹿沼	23749	2003-7-20
t0924	足利	27138	2003-7-21
t0923	佐野	71294	2003-6-8
t0934	那須烏山	62531	2003-8-17
t0926	下野	82951	2003-10-14
t0925	日光	74682	2003-4-19
t0928	矢板	57926	2003-2-12
t0927	さくら	96823	2003-5-24
t0929	真岡	64187	2003-11-14
```

## ターミナルで実行

```text
$ ./gradlew run
Path for java installation '/usr/lib/jvm/openjdk-21' (Common Linux Locations) does not contain a java executable

> Task :run
*** start ***
*** end ***

java.util.HashMap
t0931   大田原     72681   2003-9-17
t0930   栃木      82354   2003-7-04
t0922   小山      38756   2003-5-15
t0933   那須塩原    12759   2003-3-12
t0921   宇都宮     41295   2003-8-12
t0932   鹿沼      23749   2003-7-20
t0924   足利      27138   2003-7-21
t0923   佐野      71294   2003-6-8
t0934   那須烏山    62531   2003-8-17
t0926   下野      82951   2003-10-14
t0925   日光      74682   2003-4-19
t0928   矢板      57926   2003-2-12
t0927   さくら     96823   2003-5-24
t0929   真岡      64187   2003-11-14


BUILD SUCCESSFUL in 1m 5s
2 actionable tasks: 1 executed, 1 up-to-date
```