Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

안드로이드 5주차 과제 (서버 통신) #47

Open
wants to merge 1 commit into
base: hayeon-kim
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ dependencies {

implementation ("io.coil-kt:coil:1.2.0")
implementation ("io.coil-kt:coil-svg:1.2.0")

implementation ("androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1")

// ViewModel 생성함수를 편하게 사용하고 싶다면?
implementation ("androidx.fragment:fragment-ktx:1.5.3")
implementation ("androidx.activity:activity-ktx:1.6.0")
}
36 changes: 36 additions & 0 deletions app/src/main/java/com/example/gdsc/IntroductionViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.gdsc

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import retrofit2.Call
import retrofit2.Response

class IntroductionViewModel : ViewModel() {
private val _getMember: MutableLiveData<List<MemberDto>> = MutableLiveData() //read, write
val getMember: LiveData<List<MemberDto>> = _getMember //read
private val getMemberService = ServicePool.getMember

init {
getMember()
}

private fun getMember() {
getMemberService.getMember().enqueue(object : retrofit2.Callback<List<MemberDto>> {
override fun onResponse(
call: Call<List<MemberDto>>, response: Response<List<MemberDto>>
) {
if (response.isSuccessful) {
_getMember.value = response.body()
} else {
Log.d("error", "실패한 응답")
}
}

override fun onFailure(call: Call<List<MemberDto>>, t: Throwable) {
t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)"
}
})
}
}
32 changes: 32 additions & 0 deletions app/src/main/java/com/example/gdsc/WeatherActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.gdsc

import android.os.Bundle
import android.util.Log

class WeatherActivity {
private lateinit var binding: ActivityWeatherBinding
private val getWeatherService = WeatherServicePool.getWeather
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityWeatherBinding.inflate(layoutInflater)
setContentView(binding.root)
getWeatherApi()
}
private fun getTodoApi() {
getWeatherService.getWeather().enqueue(object : retrofit2.Callback<List<WeatherDto>> {
override fun onResponse(
call: Call<List<WeatherDto>>, response: Response<List<WeatherDto>>
) {
if (response.isSuccessful) {
response.body()?.let { Log.d("result", it.toString()) }
} else {
Log.d("error", "실패한 응답")
}
}

override fun onFailure(call: Call<List<WeatherDto>>, t: Throwable) {
t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)"
}
})
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/example/gdsc/WeatherApiFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.gdsc

import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Retrofit

object WeatherApiFactory {
private const val BASE_URL =
"apis.data.go.kr/1360000/VilageFcstInfoService_2.0/"

val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.build()
}
}

object WeatherServicePool {
val getWeather = WeatherApiFactory.retrofit.create(WeatherApiService::class.java)
}
24 changes: 24 additions & 0 deletions app/src/main/java/com/example/gdsc/WeatherApiService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.gdsc

import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface WeatherApiService {
@GET("/getUltraSrtNcst")
fun getUltraSrtNcst(
@Query("serviceKey") serviceKey: String = "YvHwazkj0CT00f2zDT9k3SW58W2KV4CVXA8%2BSopQPtYEMumI5QhItSGGbFMlCjruiZiFsS4nYcB0Oxfm2%2BKw3Q%3D%3D",
@Query("numOfRows") numOfRows : Int = 1,
@Query("pageNo") pageNo : Int = 1,
@Query("dataType") dataType : String = "JSON",
@Query("base_date") base_date : Int = 20231113,
@Query("base_time") base_time : Int = 0600,
@Query("nx") nx : Int = 55,
@Query("ny") ny : Int = 127
): Call<List<WeatherDto>>
}

interface WeatherApiService {
@GET("/getUltraSrtNcst?serviceKey=YvHwazkj0CT00f2zDT9k3SW58W2KV4CVXA8")
fun getWeather(): Call<List<WeatherDto>>
}
55 changes: 55 additions & 0 deletions app/src/main/java/com/example/gdsc/WeatherDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.gdsc

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class WeatherDto(
@SerialName("response")
val response: ResponseDto
)

@Serializable
data class ResponseDto(
@SerialName("header")
val header: HeaderDto,
@SerialName("body")
val body: BodyDto
)
@Serializable
data class HeaderDto(
@SerialName("resultCode")
val resultCode: Int,
@SerialName("resultMsg")
val resultMsg: String
)

@Serializable
data class BodyDto(
@SerialName("dataType")
val dataType: String,
@SerialName("items")
val items: ItemDto,
@SerialName("pageNo")
val pageNo: Int,
@SerialName("numOfRows")
val numOfRows: Int,
@SerialName("totalCount")
val totalCount: Int,
)

@Serializable
data class ItemDto(
@SerialName("base_date")
val base_date: Int,
@SerialName("base_time")
val base_time: Int,
@SerialName("category")
val category: String,
@SerialName("nx")
val nx: Int,
@SerialName("ny")
val ny: Int,
@SerialName("obsrValue")
val obsrValue: String
)
24 changes: 16 additions & 8 deletions app/src/main/java/com/example/gdsc/Week4Activity.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package com.example.gdsc
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import java.lang.reflect.Member
import android.util.Log
import com.example.gdsc.databinding.ActivityWeek4Binding
import retrofit2.Call
import retrofit2.Response


class Week4Activity : AppCompatActivity() {

private lateinit var binding: ActivityWeek4Binding
private val getMemberService = ServicePool.getMember
// private val getMemberService = ServicePool.getMember
private val viewModel by viewModels<IntroductionViewModel>()


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityWeek4Binding.inflate(layoutInflater)
setContentView(binding.root)
getMemberApi()
observeGetMember()
//getMemberApi()
}

private fun observeGetMember() {
viewModel.getMember.observe(this) { getMemberResult ->
val adapter = IntroductionAdapter()
binding.rvIntroductionGdscAndroid.adapter = adapter
adapter.submitList(getMemberResult)
}
}
/*
private fun getMemberApi() {
val adapter = IntroductionAdapter()
binding.rvIntroductionGdscAndroid.adapter = adapter
Expand All @@ -38,5 +46,5 @@ class Week4Activity : AppCompatActivity() {
t.message?.let { Log.d("error", it) } ?: "서버통신 실패(응답값 X)"
}
})
}
}*/
}