
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
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
호스트네임에 연결된 주소가 없다고 나오는데 왜 그럴까요?
