title: Kotlin retrofit2 connection using corrutines
body: <p>I was developing an App in Kotlin which connect using Retrofit2 library with the PokeApi.</p>
  <p>I'd ve tried to do it using corrutines but the Response which i've started to get are null, while before using corrutines, via asynchronous call.</p>
  <p>The Code of my dataProvider, which make the call to the API is the following:</p>
  <p>DataProvider.kt</p>
  <pre><code>   /**
       * Metodo que permite obtener un LiveData con la informacion del siguiente pokemon por id, que será la posible evolución.
       */
      fun viewPokemonEvolution(id: Long): LiveData&lt;PokemonFormResponse&gt;? {
  
          var remotePokemonEvolutionFormData : LiveData&lt;PokemonFormResponse&gt;? =  null
          var call: Response&lt;LiveData&lt;PokemonFormResponse&gt;&gt;
          var data:  LiveData&lt;PokemonFormResponse&gt;?
          CoroutineScope(Dispatchers.Main).launch {
              call = remoteDataSource.downloadPokemonViewedData(id)
              data = call.body()
              if(call.isSuccessful){
                  remotePokemonEvolutionFormData = data
              }
          }
          return remotePokemonEvolutionFormData!!
      }
  </code></pre>
  <p>My API class</p>
  <pre><code>interface PokemonApi {
  
      @GET(&quot;pokemon-form/{id}&quot;)
      suspend fun getPokemonInfo(@Path(&quot;id&quot;) idPokemon: Long): Response&lt;LiveData&lt;PokemonFormResponse&gt;&gt;
  
      @GET(&quot;pokemon/{name}&quot;)
      suspend fun getPokemonExtendedInfo(@Path(&quot;name&quot;) pokemonName: String): Response&lt;LiveData&lt;PokemonExtendedInfoResponse&gt;&gt;
  
  }
  </code></pre>
  <p>My data class</p>
  <p>PokemoFormResponse.kt</p>
  <pre><code>data class PokemonFormResponse(
      @SerializedName(&quot;form_name&quot;)
      val formName: String,
      @SerializedName(&quot;form_names&quot;)
      val formNames: List&lt;Any&gt;,
      @SerializedName(&quot;form_order&quot;)
      val formOrder: Int,
      @SerializedName(&quot;id&quot;)
      val id: Int,
      @SerializedName(&quot;is_battle_only&quot;)
      val isBattleOnly: Boolean,
      @SerializedName(&quot;is_default&quot;)
      val isDefault: Boolean,
      @SerializedName(&quot;is_mega&quot;)
      val isMega: Boolean,
      @SerializedName(&quot;name&quot;)
      val name: String,
      @SerializedName(&quot;names&quot;)
      val names: List&lt;Any&gt;,
      @SerializedName(&quot;order&quot;)
      val order: Int,
      @SerializedName(&quot;pokemon&quot;)
      val pokemon: PokemonUrl,
      @SerializedName(&quot;sprites&quot;)
      val sprites: Sprites,
      @SerializedName(&quot;version_group&quot;)
      val versionGroup: VersionGroup
  ){
      fun idFilledWithZero(): String {
          return String.format(&quot;%03d&quot;, id)
      }
  }
  </code></pre>
  <p>My remoteDatasource</p>
  <p>IRemoteDataSource.kt</p>
  <pre><code>interface IRemoteDataSource {
  
      suspend fun downloadPokemonViewedData(id: Long): Response&lt;LiveData&lt;PokemonFormResponse&gt;&gt;
      suspend fun downloadPokemonCatchedData(name: String): Response&lt;LiveData&lt;PokemonExtendedInfoResponse&gt;&gt;
  }
  
  interface ILocalDataSource {
  
      fun getPokemonList(): LiveData&lt;List&lt;Pokemon&gt;&gt;
      fun getPokemonById(idPokemon: Long): LiveData&lt;Pokemon&gt;
     suspend fun insertPokemon(pokemon: Pokemon)
  }
  </code></pre>
  <p>And the DAO which call room in the LocalDataSource:</p>
  <pre><code>@Dao
  interface PokemonDao {
  
      @Query(&quot;SELECT * from listaPokemon&quot;)
      fun getAll(): LiveData&lt;List&lt;Pokemon&gt;&gt;
  
      @Insert(onConflict = REPLACE)
      fun insert(pokemon:Pokemon)
  
      @Insert(onConflict = REPLACE)
      fun insertAll(vararg pokemons: Pokemon)
  
      @Query(&quot;SELECT * from listaPokemon WHERE id = :pokemonId&quot;)
      fun getById(pokemonId: Long): LiveData&lt;Pokemon&gt;
  
  }
  </code></pre>
  <p>I hope yu can suggest some way to fix the implementation, due to is such better way use corrutines instead of asynchronous calls.</p>
  <p>I add a capture of the debugg show all the atributes null</p>
  <p><a href="https://i.stack.imgur.com/ugajM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ugajM.png" alt="enter image description here" /></a></p>
  <p>I hope you can help, and if it's like this tke thanks in advance !</p>
  <p>[EDIT]</p>
  <p>ADDED RemoteDataSource`</p>
  <pre><code>
  class RemoteDataSource : IRemoteDataSource{
  
      val BASE_URL = &quot;https://pokeapi.co/api/v2/&quot;
      val TIMEOUT: Long = 30
  
      var apiServices: PokemonApi
  
      init {
          val httpClient : OkHttpClient.Builder = OkHttpClient.Builder()
          httpClient.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
          httpClient.readTimeout(TIMEOUT, TimeUnit.SECONDS)
          httpClient.writeTimeout(TIMEOUT, TimeUnit.SECONDS)
  
          val retrofit: Retrofit = Retrofit.Builder()
         //Se utiliza el networkIO como ejecutor de Retrofit
              .callbackExecutor(AppExecutors.networkIO)
              .client(httpClient.build())
              .addConverterFactory(GsonConverterFactory.create())
              .baseUrl(BASE_URL)
              .build()
  
          apiServices = retrofit.create(PokemonApi::class.java)
      }
  
  
      /**
       * Función tipo utilizando retrofit para obtener datos desde una api remota
       *
       * Simplicación de las funciones mediante las corrutinas
       */
      override suspend fun downloadPokemonViewedData(id: Long): Response&lt;LiveData&lt;PokemonFormResponse&gt;&gt; = withContext(Dispatchers.Main) {
          apiServices.getPokemonInfo(
              id
          )
      }
  
      override suspend fun downloadPokemonCatchedData(name: String): Response&lt;LiveData&lt;PokemonExtendedInfoResponse&gt;&gt; = withContext(Dispatchers.Main){
          apiServices.getPokemonExtendedInfo(
              name
          )
      }
  
  </code></pre>
  <p>And the interface</p>
  <p>IRemoteDatasource.kt</p>
  <pre><code>interface IRemoteDataSource {
  
      suspend fun downloadPokemonViewedData(id: Long): Response&lt;LiveData&lt;PokemonFormResponse&gt;&gt;
      suspend fun downloadPokemonCatchedData(name: String): Response&lt;LiveData&lt;PokemonExtendedInfoResponse&gt;&gt;
  }
  </code></pre>