<uses-permission android:name="android.permission.INTERNET"/>
<application
android:usesCleartextTraffic="true">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
val nw = connectivityManager.activeNetwork
val actNw = connectivityManager.getNetworkCapabilities(nw)
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
return "wifi online"
}
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
return "cellular available"
}
package com.tutorial.c85
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val resultView = findViewById<TextView>(R.id.resultView)
resultView.text = isNetworkAvailable()
}
private fun isNetworkAvailable(): String {
val manager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val nw = manager.activeNetwork ?: return "offline"
val actNw = manager.getNetworkCapabilities(nw) ?: return "offline"
return when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
return "wifi online"
}
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
return "cellular online"
}
else -> return "offline"
}
}else{
return if(manager.activeNetworkInfo?.isConnected!!) {
"online"
}else{
"offline"
}
}
}
}
๊ฐ๋ฐ์๊ฐ ๋คํธ์ํฌ ์ ํธ์ถ๋์ด์ผ ํ ํจ์๋ฅผ ๊ฐ๋ ์ธํฐํ์ด์ค๋ฅผ ๋ง๋ค์ด์ ๋ ํธ๋กํ์ ๋ฑ๋ก์ํค๋ฉด, ๋ ํธ๋กํ์ ํด๋น ํจ์์ ๋คํธ์ํน๊ณผ ๊ด๋ จ๋ ์ฝ๋๋ฅผ ์์ฑํ์ฌ Call ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค. ๊ทธ๋ฆฌ๊ณ Call ๊ฐ์ฒด์ enqueue ํจ์๋ฅผ ํธ์ถํ๋ฉด ๋คํธ์ํน์ ์์ํ ์ ์๋ค.
cf) DAO, DTO, VO๋?
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
Gson ๋ง๊ณ ๋ ๋ค์๊ณผ ๊ฐ์ด ๋ค์ํ parse๋ค์ด ์๋ค.
์ด๋ฐ parser๋ค์ JSON ๋๋ XML ๋ฐ์ดํฐ๋ฅผ ํ์ฑํ์ฌ ๋ชจ๋ธ ํด๋์ค ๊ฐ์ฒด์ ๋ด์์ฃผ๋ ๊ฒ์ ์๋ํ ํ๋ค.
// json ๋ฐ์ดํฐ
{
"id": 7,
"email": "michael.lawson@reqres.in",
"first_name": "Michael",
"last_name": "Lawson",
"avatar": "https://reqres.in/img/faces/7-image.jpg"
}
// ๋ชจ๋ธ ํด๋์ค
data class UserModel(
var id: String,
@SerializedName("first_name")
var firstName: String,
@SerializedName("last_name")
var lastName: String, var avatar: String, var avatarBitmap: Bitmap
)
interface INetworkService {
@GET("api/users")
fun getUserList(@Query("page") page: String): Call<UserListModel>
}
val retrofit: Retrofit
get() = Retrofit.Builder()
.baseUrl("https://reqres.in/")
.addConverterFactory(GsonConverterFactory.create())
.build()
var networkService: INetworkService = retrofit.create(INetworkService::class.java)
val userListCall = networkService.getUserList("1")
userListCall.enqueue(object : Callback<UserListModel> {
override fun onResponse(call: Call<UserListModel>, response: Response<UserListModel>) {
val userList = response.body()
}
override fun onFailure(call: Call<UserListModel>, t: Throwable) {
call.cancel()
}
})
์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ๊ฒ์ ์ด ์ฌ์ดํธ๋ฅผ ์ด์ฉํ์ฌ ํ ์คํธ ํด๋ณผ ์์ ์ด๋ค.
๋ชจ๋ ๋จ์์ build.gradle ํ์ผ
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
AndroidManifest.xml ํ์ผ
<uses-permission android:name="android.permission.INTERNET"/>
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
package com.tutorial.c87
import com.google.gson.annotations.SerializedName
data class UserModel (
@SerializedName("first_name")
var firstName: String,
@SerializedName("last_name")
var lastName: String
)
package com.tutorial.c87
data class UserListModel (
var data: List<UserModel>?
)
package com.tutorial.c87
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query
interface INetworkService {
@GET("api/users")
fun getUserList(@Query("page") page: String): Call<UserListModel>
}
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
package com.tutorial.c87
import android.os.Bundle
import android.widget.ListView
import android.widget.SimpleAdapter
import androidx.appcompat.app.AppCompatActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class MainActivity : AppCompatActivity() {
private val retrofit: Retrofit
get() = Retrofit.Builder()
.baseUrl("https://reqres.in/")
.addConverterFactory(GsonConverterFactory.create())
.build()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listView = findViewById<ListView>(R.id.listView)
val networkService = retrofit.create(INetworkService::class.java)
val call = networkService.getUserList("1")
call.enqueue(object: Callback<UserListModel> {
override fun onResponse(call: Call<UserListModel>, response: Response<UserListModel>) {
val userList = response.body()
val mutableList = mutableListOf<Map<String, String>>()
userList?.data?.forEach {
val map = mapOf("firstName" to it.firstName, "lastName" to it.lastName)
mutableList.add(map)
}
val adapter = SimpleAdapter(
this@MainActivity,
mutableList,
android.R.layout.simple_expandable_list_item_2,
arrayOf("firstName", "lastName"),
intArrayOf(android.R.id.text1, android.R.id.text2)
)
listView.adapter = adapter
}
override fun onFailure(call: Call<UserListModel>, t: Throwable) {
call.cancel()
}
})
}
}
cf) ๋ ํธ๋กํ์ผ๋ก๋ ์๋ฒ๋ก๋ถํฐ ์ด๋ฏธ์ง๋ฅผ ๋ฐ์์ฌ ์ ์๋ค. ํ์ง๋ง, Glide์ ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ํ ์ ๋ฌธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ํจ์ฌ ํธํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ๋ฒ์ ์์๋ณด์!
implementation 'com.github.bumptech.glide:glide:4.11.0'
Glide.with(this)
.load(R.drawable.seoul) // ๋ฆฌ์์ค ์ด๋ฏธ์ง
.into(resultView)
Glide.with(this)
.load(url) // ์๋ฒ ์ด๋ฏธ์ง
.into(resultView)
Glide.with(this)
.load(R.drawable.seoul)
.override(200, 200) // ์ฌ์ด์ฆ ์ง์
.into(resultView)
Glide.with(this)
.load(url) // ์๋ฒ ์ด๋ฏธ์ง
.override(200, 200)
.placeholder(R.drawable.loading) // ๋ก๋ฉ ์ด๋ฏธ์ง
.error(R.drawable.error) // ์๋ฌ ์ด๋ฏธ์ง
.into(resultView)
implementation 'com.github.bumptech.glide:glide:4.11.0'
<uses-permission android:name="android.permission.INTERNET"/>
package com.tutorial.c88
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val resultView = findViewById<ImageView>(R.id.resultView)
Glide.with(this)
.load("https://www.edigitalagency.com.au/wp-content/uploads/google-logo-png-transparent-background-large-new.png")
.override(200, 200)
.placeholder(R.drawable.loading)
.error(R.drawable.error)
.into(resultView)
}
}
์ ์์ ์ผ๋ก ์คํ๋ ๊ฒฝ์ฐ
URL ์ฃผ์๊ฐ ์๋ชป๋ ๊ฒฝ์ฐ