martin suchodol
09/11/2022, 8:50 AMUnable to create converter for retrofit2.Call<com.marsuch.weatherapp.network.WeatherInfo>
for method WeatherApiService.getWeatherData
I have found a number of different solutions on the internet however none of them work for me. I use Moshi with retrofit.
My WeatherInfo.kt
data class WeatherInfo (
val city : City,
val cod : Int,
val message : String,
val cnt : Int,
//val list : List<Weather>,
)
data class City (
val id : Int,
val name : String,
//val coord : Coord,
val country : String,
val population : Int,
val timezone: String?
)
WeatherApiServices.kt
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query
private const val BASE_URL = "<http://api.openweathermap.org/data/2.5/>"
private var lon: String = ""
private var lat: String = ""
private var appId: String = ""
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(BASE_URL)
.build()
interface WeatherApiService {
@GET("forecast/daily")
suspend fun getWeatherData(
@Query("lat") lat : String,
@Query("lon") lon : String,
@Query("appid") apiKey : String
): Call<WeatherInfo>
}
object WeatherApi{
val retrofitService : WeatherApiService by lazy {
retrofit.create(WeatherApiService::class.java)
}
}
The resulting data are displayed as follows:
private val _status = MutableLiveData<String>()
fun getData() {
viewModelScope.launch {
try {
val listResult = WeatherApi.retrofitService.getWeatherData(lat, lon, api)
//_status.value = "Success ${listResult} wether data retrieved"
Log.d("Response weather:", listResult.toString())
}
catch (e: Exception) {
_status.value = "Failure: ${e.message}"
Log.e("Failure", e.message.toString())
}
}
}
I'm still learning, so I apologize if the solution to the error is trivial.Chrimaeon
09/11/2022, 9:02 AMKotlin-reflect
library?martin suchodol
09/11/2022, 10:15 AMChrimaeon
09/11/2022, 10:26 AMmartin suchodol
09/11/2022, 11:29 AM