Hi, I need help with this error: `Unable to create converter for retrofit2.Call<com.marsuch.weath...
m

martin suchodol

over 2 years ago
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
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.
😶 3
🧵 3
Hello, anyone can help me with this error? ```Exception in thread "DefaultDispatcher-worker-6 @track...
g

Gabi

almost 3 years ago
Hello, anyone can help me with this error?
Exception in thread "DefaultDispatcher-worker-6 @track-session/cu#80" java.lang.NullPointerException: Cannot invoke "kotlinx.coroutines.flow.Flow.collect(kotlinx.coroutines.flow.FlowCollector, kotlin.coroutines.Continuation)" because "this.$this_unsafeTransform$inlined" is null
	at kotlinx.coroutines.flow.FlowKt__TransformKt$onEach$$inlined$unsafeTransform$1.collect(SafeCollector.common.kt:113)
	at kotlinx.coroutines.flow.FlowKt__CollectKt.collect(Collect.kt:30)
	at kotlinx.coroutines.flow.FlowKt.collect(Unknown Source)
	at gragas.play.TrackSession.playRegisteredTracks(TrackSession.kt:117)
	at gragas.play.TrackSession.access$playRegisteredTracks(TrackSession.kt:38)
	at gragas.play.TrackSession$3.invokeSuspend(TrackSession.kt:58)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
	Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [CoroutineName(track-session/cu), CoroutineId(80), "track-session/cu#80":StandaloneCoroutine{Cancelling}@6465a67d, Dispatchers.Default]
This is the source of playRegisteredTracks
private suspend fun playRegisteredTracks() {
  queue
    .onEach { song ->
      player.playTrack(song)
    }
    .collect()
}
And this is the full source of TrackSession.kt https://gist.github.com/2cc063411159091c45c3a3e1d4cc8155