Hi, I need help with this error: `Unable to create...
# android
m
Hi, I need help with this error:
Unable 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
Copy code
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
Copy code
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:
Copy code
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.
😶 3
🧵 3
c
When using reflection mentioned here did you add the
Kotlin-reflect
library?
m
No I based it on this tutorial: https://developer.android.com/codelabs/basic-android-kotlin-training-getting-data-inter[…]labs%2Fbasic-android-kotlin-training-getting-data-internet There, nothing like that was used. The source json from the tutorial, however, is much simpler. But I assumed it wouldn't have any effect.
c
So rather follow the description of the library itself than a tutorial that might have missed something or just be outdated. Or get sure you are using exactly the same versions for everything as in the tutorial.
👆 1
👍 1
m
So I finally discovered the cause of the problem, which was the suspended getWeatherData method of the WatherApiService interface. When I tried to use suspend and call at the same time.
👍 2
2501 Views