facebook twitter hatena line email

「Android/kotlin/Retrofit2」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
行118: 行118:
 
</pre>
 
</pre>
  
 +
出力ログ
 
<pre>
 
<pre>
 
login=googleads
 
login=googleads

2021年1月7日 (木) 18:22時点における最新版

Retrofitとは

APIやり取りのライブラリ

サンプル

app/build.gradle

dependencies{
    def retrofitVer='2.9.0'
    implementation"com.squareup.retrofit2:retrofit:$retrofitVer"
    implementation"com.squareup.retrofit2:converter-gson:$retrofitVer"
}

app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

GitHubClient.kt

package com.example.retrofit2application

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Path

interface GitHubClient {

    @Headers(
        "Accept: application/vnd.github.v3.full+json",
        "User-Agent: Retrofit2Agent"
    )
    @GET("/users/{username}")
    fun getUser(@Path("username") username: String) : Call<User>
}

User.kt

package com.example.retrofit2application

data class User(
    val login: String,
    val id: Int,
    val avatar_url: String,
    val gravatar_id: String,
    val url: String,
    val html_url: String,
    val followers_url: String,
    val following_url: String,
    val gists_url: String,
    val starred_url: String,
    val subscriptions_url: String,
    val organizations_url: String,
    val repos_url: String,
    val events_url: String,
    val received_events_url: String,
    val type: String,
    val site_admin: Boolean,
    val name: String,
    val company: String?,
    val blog: String?,
    val location: String?,
    val email: String?,
    val hireable: Boolean?,
    val bio: String?,
    val public_repos: Int,
    val public_gists: Int,
    val followers: Int,
    val following: Int,
    val created_at: String,
    val updated_at: String
)
package com.example.retrofit2application

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MainActivity : AppCompatActivity() {
    val tag = "MainActivity"

    val retrofit = Retrofit.Builder()
            .baseUrl("https://api.github.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

    var service = retrofit.create(GitHubClient::class.java)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        service.getUser("googleads")
                .enqueue(object : Callback<User> {
                    override fun onResponse(call: Call<User>, response: Response<User>) {
                        if (response.isSuccessful) {
                            val user: User? = response.body()
                            if (user != null) {
                                Log.d(tag, "login=" + user.login)
                                Log.d(tag, "type=" + user.type)
                                Log.d(tag, "name=" + user.name)
                            }
                        }
                    }
                    override fun onFailure(call: Call<User>, t: Throwable) {
                    }
                })
    }
}

出力ログ

login=googleads
type=Organization
name=Google Ads

参考

https://wiki.toridge.com/index.php?android-kotlin-retrofit2

https://qiita.com/175atsu/items/573193af65b91aa79df5

https://qiita.com/SYABU555/items/3b280a8e81d2cc897383