๋ ์จ ์ ๋ณด ๊ณต๊ณต๋ฐ์ดํฐ๊ฐ ๋ฏธ์ธ๋จผ์ง๋ณด๋ค ์ ์์ค์ ๋ชฐ๋๋๋ฐ...ใ
๋ํ ์ผ ํ์ด์ง์ ๋์ค๋ ์ ํ๋ ์ฐ์ ์์น๋ฅผ ํ์ ํ๊ณ , ์ด ์ฐ์ ์ง์ญ ๋ ์จ๋ฅผ ๋ณด์ฌ์ค ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๊ธฐ์์ฒญ์์ api๋ฅผ ๊ฐ์ ธ์ค๋๋ก api๋ฅผ ์ฐ๊ฒฐํด์ค๋ค.
interface WeatherInterface {
@GET("getVilageFcst")
suspend fun getWeatherInfo(
@Query("serviceKey") serviceKey : String,
@Query("pageNo") pageNo : String,
@Query("dataType") dataType : String,
@Query("base_date") base_data : String,
@Query("base_time") base_time : String,
@Query("nx") nx : String,
@Query("ny") ny : String
)
}
object WeatherClient {
private val retrofit = Retrofit.Builder()
.baseUrl(Constrant.WEATHER_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService = retrofit.create(WeatherInterface::class.java)
}
data class Root(
val response: Response,
)
data class Response(
val header: Header,
val body: Body,
)
data class Header(
val resultCode: String,
val resultMsg: String,
)
data class Body(
val dataType: String,
val items: Items,
val pageNo: Long,
val numOfRows: Long,
val totalCount: Long,
)
data class Items(
val item: List<Item>,
)
data class Item(
val baseDate: String,
val baseTime: String,
val category: String,
val fcstDate: String,
val fcstTime: String,
val fcstValue: String,
val nx: Long,
val ny: Long,
)
๊ฐ์ ธ์จ ๊ณต๊ณต๋ฐ์ดํฐ ์ฌ์ดํธ๋ ๊ธฐ์์ฒญ๋จ๊ธฐ์๋ณด ((๊ตฌ)๋๋ค์๋ณด) ์กฐํ์๋น์ค
์ด ๋ฐ์ดํฐ์์์ ์ฐธ์กฐ๋ฌธ์๋ฅผ ํตํด ์ํ๋ฐ์ดํฐ๊ฐ ์ ํํ ์ด๋ค ๊ฒ์ ๊ฐ๋ฆฌํค๋์ง ํ์ธํด๋ณผ์ ์๋ค. ์๋ ์ ๋ณด๋ ์ฐธ์กฐ๋ฌธ์ ์ค ๋จ๊ธฐ์๋ณด์ ๊ด๋ จ๋ ์ฐธ๊ณ ๋ฌธ์ ๋ด์ฉ ์ค ์ผ๋ถ์ด๋ค.
์ํ๋ฐ์ดํฐ์ ๋น๊ต๋ฅผ ํด๋ณด๋ฉด์ ์ ๋ฆฌํด๋ณด์.
<response>
<header>
<resultCode>00</resultCode>
<resultMsg>NORMAL_SERVICE</resultMsg>
</header>
<body>
<dataType>XML</dataType>
<items>
<item>
<baseDate>20231023</baseDate>
<baseTime>0500</baseTime>
<category>TMP</category>
<fcstDate>20231023</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>12</fcstValue>
<nx>55</nx>
<ny>127</ny>
</item>
<item>
<baseDate>20231023</baseDate>
<baseTime>0500</baseTime>
<category>UUU</category>
<fcstDate>20231023</fcstDate>
<fcstTime>0600</fcstTime>
<fcstValue>-0.4</fcstValue>
<nx>55</nx>
<ny>127</ny>
</item>
<item>
<baseDate>20231023</baseDate>
<baseTime>0500</baseTime>
<category>TMX</category>
<fcstDate>20231025</fcstDate>
<fcstTime>1500</fcstTime>
<fcstValue>21.0</fcstValue>
<nx>55</nx>
<ny>127</ny>
</item>
...
์ฌ์ฉํ ํญ๋ชฉ๊ฐ์ 1์๊ฐ ๊ธฐ์จ(TMP)์ด๋ ์ผ ์ต๊ณ ๊ธฐ์จ(TMX)์ ์ฌ์ฉํด์ค ๊ฒ์ด๋ค.
Base_time์ผ๋ก ์ค์ ํด์ค ์๊ฐ์ผ๋ก๋ ์์งํ ๋ฑ์ฐ์ ๋๊ฐ ์๋ฒฝ 2์์ ํ ๊น ์ถ์ง๋ง...์ผ๋จ ์๊ฐ์ ๋ค ์ถ๊ฐํด์ค ์์ ์ด๋ค.
๋น๋๊ธฐ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ๊ฑด๋ฐ api ๋ฐ์ดํฐ๋ฅผ ์ ๋๋ก ๊ฐ์ ธ์ค๋์ง ํ์ธํ๊ธฐ ์ํด ์ผ๋จ baseDate
์ nx
, ny
๋ฅผ ๊ณ ์ ์ ์ผ๋ก ์ค์ ํด์ฃผ๊ณ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์๋ณด์.
TMP
์ ๋ฐ๋ฅธfcstValue
๊ฐ์ ๊ฐ์ ธ์ ๋ก๊ทธ๋ฅผ ํตํด ์ ์ฐํ๋์ง ํ์ธํด์ฃผ๋ฉด ๋!
CoroutineScope(Dispatchers.IO).launch {
WeatherClient.weatherNetwork.getWeatherInfo(
serviceKey = BuildConfig.WEATHER_API_KEY,
pageNo = 1,
numOfRows = 10,
dataType = "JSON",
baseDate = 20231023,
baseTime = 0200,
nx = 55,
ny = 125,
).enqueue(object : Callback<Weather?> {
override fun onResponse(call: Call<Weather?>, response: Response<Weather?>) {
response.body().let {
it?.response?.body?.items?.item?.forEach { item ->
if (item.category == "TMP") {
val tmpValue = item.fcstValue
weatherDataList.add(WeatherData(baseTime, tmpValue))
Log.d("text","baseDate : ${item.baseDate},
baseTime : ${item.baseTime},
category : ${item.category}")
Log.d("text","fxstDate : ${item.fcstTime},
fxstDate : ${item.fcstDate},
fxstValue : ${item.fcstValue}")
Log.d("text", "nx : ${item.nx}, ny : ${item.ny}")
}
}
}
override fun onFailure(call: Call<Weather?>, t: Throwable) {
Log.e("error", "${t.message}")
}
})