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

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.



Logcat을 살펴보면 okhttp.OkHttpClient 링크된 Url 에서 기상데이터는 잘 불러온 것 같은데


WeatherViewModel,kt 에 "호스트 이름과 연결된 주소 없음" 라고 나오는데 왜 그럴까요?



작성한 코드 및 에러 메세지

package com.example.spartaweatherapp.viewmodel

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.spartaweatherapp.data.WeatherData
import com.example.spartaweatherapp.repository.WeatherRepository
import kotlinx.coroutines.launch
import retrofit2.HttpException
import java.io.IOException

private const val TAG = "WeatherViewModel"

class WeatherViewModel(private val repository: WeatherRepository = WeatherRepository()) : ViewModel() {
    private val _regionText = MutableLiveData("서울특별시")
    val regionText: LiveData<String> = _regionText

    fun setRegion(city: String) {
        _regionText.value = city
    }

    private val _weatherData = MutableLiveData<WeatherData>()
    val weatherData get() = _weatherData

    private val _weatherList = MutableLiveData<List<WeatherData>>()
    val weatherList get() = _weatherList

    fun getWeather(date: String, city: String = regionText.value ?: "서울특별시") {
        viewModelScope.launch {
            runCatching {
                repository.getWeather(date, city)
            }.onSuccess { weatherResponse ->
                _weatherData.value = weatherResponse.toWeatherData()
            }.onFailure { e ->
                handleException(e)
            }
        }
    }

    fun getWeatherList(date: String, count: Int = 20) {
        viewModelScope.launch {
            runCatching {
                val city = regionText.value ?: "서울특별시"
                repository.getWeather(date, city, pageNo = count)
            }.onSuccess { weatherResponse ->
                _weatherList.value = weatherResponse.toWeatherList(count)
            }.onFailure { e ->
                handleException(e)
            }
        }
    }

    private fun handleException(e: Throwable) {
        when (e) {
            is HttpException -> {
                val errorJsonString = e.response()?.errorBody()?.string()
                Log.e(TAG, "HTTP error: $errorJsonString")
            }

            is IOException -> Log.e(TAG, "Network error: $e")
            else -> Log.e(TAG, "Unexpected error: $e")
        }
    }
}


Logcat Error -->

Network error: java.net.UnknownHostException: Unable to resolve host "apis.data.go.kr": No address associated with hostname


호스트네임에 연결된 주소가 없다고 나오는데 왜 그럴까요?

첨부파일 (1)

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