0
0

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 1 year has passed since last update.

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

Last updated at Posted at 2023-11-05

こちらのプログラムを IntelliJ から AndroidStudio に移しました。
IntelliJ: Retrofit で HTTP リクエスト (HashMap)

image.png

環境

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

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")
}

画面

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

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)
    }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?