๐ŸŒ„ Sandamso ํ”„๋กœ์ ํŠธ - 6

kkominยท2023๋…„ 10์›” 23์ผ
0

Android Studio

๋ชฉ๋ก ๋ณด๊ธฐ
42/44
post-thumbnail

๋‚ ์”จ ์ •๋ณด ๊ณต๊ณต๋ฐ์ดํ„ฐ๊ฐ€ ๋ฏธ์„ธ๋จผ์ง€๋ณด๋‹ค ์ ์„์ค„์€ ๋ชฐ๋ž๋Š”๋ฐ...ใ… 


Weather API ์—ฐ๊ฒฐ

๋””ํ…Œ์ผ ํŽ˜์ด์ง€์— ๋‚˜์˜ค๋Š” ์„ ํƒ๋œ ์‚ฐ์˜ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•˜๊ณ , ์ด ์‚ฐ์˜ ์ง€์—ญ ๋‚ ์”จ๋ฅผ ๋ณด์—ฌ์ค„ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์ƒ์ฒญ์—์„œ api๋ฅผ ๊ฐ€์ ธ์˜ค๋„๋ก api๋ฅผ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

Retrofit

Interface ์ƒ์„ฑ

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

Client ์ƒ์„ฑ

  object WeatherClient {
      private val retrofit = Retrofit.Builder()
          .baseUrl(Constrant.WEATHER_BASE_URL)
          .addConverterFactory(GsonConverterFactory.create())
          .build()

      val apiService  = retrofit.create(WeatherInterface::class.java)
  }

Model Data ์ƒ์„ฑ

  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,
  )

API ์ •๋ณด ์‚ดํŽด๋ณด๊ธฐ

๊ฐ€์ ธ์˜จ ๊ณต๊ณต๋ฐ์ดํ„ฐ ์‚ฌ์ดํŠธ๋Š” ๊ธฐ์ƒ์ฒญ๋‹จ๊ธฐ์˜ˆ๋ณด ((๊ตฌ)๋™๋„ค์˜ˆ๋ณด) ์กฐํšŒ์„œ๋น„์Šค

์ด ๋ฐ์ดํ„ฐ์—์„œ์˜ ์ฐธ์กฐ๋ฌธ์„œ๋ฅผ ํ†ตํ•ด ์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ๊ฐ€ ์ •ํ™•ํžˆ ์–ด๋–ค ๊ฒƒ์„ ๊ฐ€๋ฆฌํ‚ค๋Š”์ง€ ํ™•์ธํ•ด๋ณผ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์ •๋ณด๋Š” ์ฐธ์กฐ๋ฌธ์„œ ์ค‘ ๋‹จ๊ธฐ์˜ˆ๋ณด์™€ ๊ด€๋ จ๋œ ์ฐธ๊ณ ๋ฌธ์„œ ๋‚ด์šฉ ์ค‘ ์ผ๋ถ€์ด๋‹ค.

์ƒ˜ํ”Œ๋ฐ์ดํ„ฐ์™€ ๋น„๊ต๋ฅผ ํ•ด๋ณด๋ฉด์„œ ์ •๋ฆฌํ•ด๋ณด์ž.

<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}")
                      }
                  })
              
         
profile
์†Œ์†Œํ•œ ์ฝ”๋”ฉ ์ผ๊ธฐ

0๊ฐœ์˜ ๋Œ“๊ธ€