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

こちらのプログラムを IntelliJ から AndroidStudio に移しました。
[IntelliJ: Retrofit で HTTP リクエスト (HashMap)](https://qiita.com/ekzemplaro/items/0784fa0e145c77641b2f)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/090edf9d-efc7-f808-13db-78eee97394a7.png)

## 環境
インターネットに接続する

```xml:AndroidManifest.xml
（省略）
<uses-permission android:name="android.permission.INTERNET" />
（省略）
```

```app/build.gradle.kts
（省略）

dependencies {
（省略）

    implementation("com.squareup.retrofit2:retrofit:2.9.0")
    implementation("com.squareup.retrofit2:converter-gson:2.9.0")
}
```

画面

```xml:layout/;activity_player.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="409dp"
        android:layout_height="729dp"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView"
        android:textSize="40sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
```

## プログラム
ツリー構造
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/bb81ede2-aad5-d69e-22a0-7777cdc05432.png)

```MainActivity.kt
package com.example.get06

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import com.example.get06.service.CityService
import com.example.get06.service.CityServiceImpl

class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView = findViewById<TextView>(R.id.textView)
        textView.text = "Good Evening"

        getAllCitiesUsingRetrofit()
    }


    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)
                textView.text = 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.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
    }

}
```

```service/CityService.kt
package com.example.get06.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 com.example.get06.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)
    }
}
```