0

Necesito llenar el arraylist con lo que me esta devolviendo retrofit en onResponse cuando hago el debug logro ver los datos pero cuando los retorno la lista esta vacia, parece que esta retornado cuando aun no se hace la llamada a la api. como podria solucionarlo?

  fun ListRecipe() : ArrayList<RecipeObj> {
        var obj: ArrayList<RecipeObj> = ArrayList<RecipeObj>()
        var recetas: RecipeObj?
      //  RecipeLiveData = MutableLiveData<hitsobj>()
        val retrofit: Retrofit = Retrofit.Builder()
                .baseUrl(Credentials.Url)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
        val service: ApiService = retrofit.create<ApiService>(ApiService::class.java)
        service.getAllRecipe("rice", Credentials.App_Id, Credentials.App_Key).enqueue(object : Callback<RecipeObj> {
            override fun onResponse(
                    call: Call<RecipeObj>, response: Response<RecipeObj>?) {
                 recetas = response!!.body()
                var list = listOf(recetas)
             
                obj.add(recetas!!)
               
            }
            override fun onFailure(call: Call<RecipeObj>, t: Throwable) {
                Log.e(TAG, t.message.toString())
            }
        })
        return(obj)
    }
2
  • proandroiddev.com/… el 27 dic. 2020 a las 19:49
  • As the previous comment said, you need some way to handle the asynchronous approach... so with your approach always return an empty list because the call is not finished yet but you are returning the empty list value... a classic way to handle this is using a callback el 31 dic. 2020 a las 11:39

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.