커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
4-5 실습 B-2 : 오늘 날씨 서비스 앱 실행에러입니다.
[왕초보] Kotlin으로 만드는 공공데이터 활용 안드로이드 앱
4주차
북마크
남*호
댓글
7
추천
0
조회수
30
조회수
30
답변 완료


install에 성공하여 도시 탭버튼도 작동하고 Home, List 전환도 되지만

Home fragment에 기상데이터를 표시하지 못하고,List의 시간별 기상데이터도 표시되지 안습니다.


스파르타 즉문즉답


스파르타 즉문즉답



작성한 코드 및 에러 메세지


package com.example.spartaweatherapp.model

import android.util.Log
import com.example.spartaweatherapp.RainStatus
import com.example.spartaweatherapp.SkyStatus
import com.example.spartaweatherapp.WeatherData
import com.google.gson.annotations.SerializedName

data class WeatherModel(
    @SerializedName("responce")
    val response: Response
) {
    fun toWeatherData(): WeatherData {
        return response.body.items.item.toWeatherData()
    }

    private fun Any.toWeatherData(): WeatherData {
        return toWeatherData()
    }
    private fun List<Item>.toWeatherData(): WeatherData {
        val items = this
        val time = items.find { it.category == "SKY" }?.fcstTime ?: "--:--"
        val skyStatus = items.find { it.category == "SKY" }?.fcstValue ?: ""
        val rainStatus = items.find { it.category == "PTY" }?.fcstValue ?: ""
        val rainPercent = items.find { it.category == "POP" }?.fcstValue ?: ""
        val rainAmount = items.find { it.category == "PCP" }?.fcstValue ?: ""
        val temp = items.find { it.category == "TMP" }?.fcstValue ?: ""
        val windSpeed = items.find { it.category == "WSD" }?.fcstValue ?: ""
        val humidity = items.find { it.category == "REH" }?.fcstValue ?: ""
        return WeatherData(
            time = time,
            skyStatus = SkyStatus.entries.firstOrNull { it.value == skyStatus.toInt() }
                ?: SkyStatus.GOOD,
            rainAmount = rainAmount,
            rainPercent = rainPercent,
            rainState = RainStatus.entries.firstOrNull { it.value == rainStatus.toInt() }
                ?: RainStatus.NONE,
            temperature = temp,
            windSpeed = windSpeed,
            humidity = humidity
        )
    }

    fun toWeatherList(count: Int): List<WeatherData> {
        val list = mutableListOf<WeatherData>()
        val items = response.body.items.item
        val baseTime = items.first()?.baseTime
        var nextTime = baseTime?.nextTime()
        repeat(count) {
            val subItems = items.filter { it!!.fcstTime == nextTime }.toList()
            val weatherData = subItems.toWeatherData()
            Log.d("WeatherData", "time: $nextTime, data: $weatherData")
            list.add(weatherData)
            nextTime = nextTime!!.nextTime()
        }
        return list
    }

    private fun String.nextTime(): String {
        if (this.length != 4) {
            throw IllegalArgumentException("잘못된 시간 형식")
        }

        val hour = this.substring(0, 2).toInt()
        val minute = this.substring(2, 4).toInt()

        if (hour !in 0..23 || minute !in 0..59) {
            throw IllegalArgumentException("잘못된 시간 범위")
        }

        val nextHour = if (hour == 23) 0 else hour + 1
        return "%02d%02d"


  RetrofitInstance.ktUrl 첨부
------------------------------------------
object RetrofitInstance {
    private const val BASE_URL = "http://apis.data.go.kr/1360000/VilageFcstInfoService_2.0"


WeatherService.kt 에 API_ KEY 발급받은 첨부
----------------------------------------------
 private const val API_KEY = ""PVTuVfsMbN%2FLK%2Bdn*********RuuH3lCD28HfPLvIyP5ARMVLFzm4S3M9cc7gYsuRCQsg6jsvWwnEXueZFiPDw%3D%3D""

Logcat 에러에서

스파르타 즉문즉답



WetherModel.kt 에서 toWeatherData 가 에러표시가 있어서 private fun 만든 것이 잘못된 것 같습니다.


첨부파일 (1)

LogCat4_5.pdf
(10.422K)
취소
 공유
취소
댓글 0
댓글 알림
나의얼굴