Wait for retrofit to answer a call?

1

I have to do a Retrofit Call inside an adapter to compare information from two arraylist:

public void checkguardadas(int id_usuario) {

    RetrofitService retrofitService = RetrofitService.getInstance();
    PabloAPI api = retrofitService.getApiProxyServer();
     Call<ArrayList<Oferta>> call = api.getGuardadas(1);

     call.enqueue(new Callback<ArrayList<Oferta>>() {
        @Override
        public void onResponse(Call<ArrayList<Oferta>> call, Response<ArrayList<Oferta>> response) {

            Log.d("traza", "por aqui");
            Log.d("traza", response.body().toString());
            guardadas = response.body();


        }

        @Override
        public void onFailure(Call<ArrayList<Oferta>> call, Throwable t) {
            Log.d("traza", "por alla");
            Log.d("traza", t.toString());
        }
    }); 

And so, I call this later.

public void onBindViewHolder(final OfertasAdapter.MyViewHolder viewHolder, 
int i) {
    Iterator it = guardadas.iterator();
    while (it.hasNext()) {
        if (ofertaList.get(i).getId() == guardadas.get(i).getId()) {
            viewHolder.guardar.setChecked(true);
        }
    }

The problem is that the call is not executed before the code is run, so it gives null value and the application does not open. Any idea what I can do? I have tried several things without much luck (AsyncTask and a dependency Rxjava) and also put all the adapter in onResponse () but I would not know how to do it. Please take into account that I am a beginner!

    
asked by Diego Romero 30.11.2017 в 11:31
source

1 answer

1

Retrofit executes the onResponse method asynchronously, so you need an event to run when the onResponse method gets the answer.

Create an interface in your project, call it OnOfertasResponse :

public interface OnOfertasResponse
{
    void ofertas(ArrayList<Oferta> ofertas);
}

The ofertas method will be executed when you get the offers from the server. This is called callback.

Now modify the method checkguardadas to accept the callback and in the method onResponse we execute the method ofertas :

public void checkguardadas(int id_usuario, OnOfertasResponse callback) {
    RetrofitService retrofitService = RetrofitService.getInstance();
    PabloAPI api = retrofitService.getApiProxyServer();
    Call<ArrayList<Oferta>> call = api.getGuardadas(1);

    call.enqueue(new Callback<ArrayList<Oferta>>() {
        @Override
        public void onResponse(Call<ArrayList<Oferta>> call, Response<ArrayList<Oferta>> response) {

            Log.d("traza", "por aqui");
            Log.d("traza", response.body().toString());

            // ejecutamos el callback
            callback.ofertas(response.body());
        }

        @Override
        public void onFailure(Call<ArrayList<Oferta>> call, Throwable t) {
            Log.d("traza", "por alla");
            Log.d("traza", t.toString());
        }
    }); 
}

Then to use this you will only have to send the callback to the method checkguardadas :

public void onBindViewHolder(final OfertasAdapter.MyViewHolder viewHolder, int i) {

    // le enviamos el callback al metodo checkguardadas
    checkguardadas(11,new OnOfertasResponse(){

        @Override
        public void ofertas(ArrayList<Oferta> ofertas){
            // este metodo se ejecutara cuando onResponse se ejecute
            Iterator it = ofertas.iterator();
            while (it.hasNext()) {
                if (ofertaList.get(i).getId() == ofertas.get(i).getId()) {
                    viewHolder.guardar.setChecked(true);
                }
            }
        }
    })
    
answered by 30.11.2017 / 13:22
source