Cómo saber cuándo finaliza una llamada de Retrofit

¿Hay alguna manera de saber cuándo mi llamada de Retrofit ha terminado es deber? Me gusta saber cuándo se reciben todos los datos para que el código pueda seguir adelante, como iniciar otra actividad o utilizar los datos de la primera llamada para hacer un segundo?

Ps .: Estoy utilizando una solicitud asíncrona (.enqueue).

Editar:

getContact(){ //Get a contact List from the server Call<List<ModelContact>> callM = contactInterface.createRContact(listContact); callM.enqueue(new Callback<List<ModelContact>>() { @Override public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) { // Fetch the List from Response and save it on the local database } @Override public void onFailure(Throwable t) { Log.i("TAG", "Error: " + t.getMessage()); } }); //Gets a object containing some configurations Call<Configs> callC = contactInterface.getConfigs(Configs); callC.enqueue(new Callback<Configs>() { @Override public void onResponse(Response<Configs> response, Retrofit retrofit) { // Fetch the Configs object and save it on the database } @Override public void onFailure(Throwable t) { Log.i("TAG", "Error: " + t.getMessage()); } }); //Here I need to start a new activity after the calls Intent loginact = new Intent(TokenActivity.this, LoginActivity.class); startActivity(loginact); } 

Tal vez usted puede usar dos banderas booleanas y comenzar la nueva intención fuera de su método getContact.

Algo como esto:

 public class MyActivity extends Activity { //lot of code omitted private boolean cIsFinished; private boolean mIsFinished; private void getContact(){ //create M and C m.onResponse(){ //do whatever you want with data and call startIntent mIsFinished=true; startIntent(); } c.onResponse(){ //do whatever you want with data and call startIntent cIsFinished=true; startIntent(); } } private synchronized void startIntent(){ if(cIsFinished && mIsFinished){ //startIntentHere! Intent intent = new blah blah blah } } } 

Movimiento

 //Gets a object containing some configurations Call<Configs> callC = contactInterface.getConfigs(Configs); callC.enqueue(new Callback<Configs>() { @Override public void onResponse(Response<Configs> response, Retrofit retrofit) { // Fetch the Configs object and save it on the database Intent loginact = new Intent(TokenActivity.this, LoginActivity.class); startActivity(loginact); } @Override public void onFailure(Throwable t) { Log.i("TAG", "Error: " + t.getMessage()); } }); 

Dentro del método onResponse como este. De esa manera callM se ejecutará, siempre que termine callC se ejecutará, y cuando callC termina, lanzará el Intent.

 getContact(){ //Get a contact List from the server Call<List<ModelContact>> callM = contactInterface.createRContact(listContact); callM.enqueue(new Callback<List<ModelContact>>() { @Override public void onResponse(Response<List<ModelContact>> response, Retrofit retrofit) { // Fetch the List from Response and save it on the local database //Gets a object containing some configurations Call<Configs> callC = contactInterface.getConfigs(Configs); callC.enqueue(new Callback<Configs>() { @Override public void onResponse(Response<Configs> response, Retrofit retrofit) { //Fetch the Configs object and save it on the database //Here I need to start a new activity after the calls Intent loginact = new Intent(TokenActivity.this, LoginActivity.class); startActivity(loginact); } @Override public void onFailure(Throwable t) { Log.i("TAG", "Error: " + t.getMessage()); } }); } @Override public void onFailure(Throwable t) { Log.i("TAG", "Error: " + t.getMessage()); } }); } 

Si usted tiene cualquier petición dependen el uno al otro, será compleja. Puede hacerlo con RxJava utilizando el método flatMap para encadenar solicitud múltiple y evitar el infierno de devolución de llamada. Es bastante simple. Puedes aprenderlo aquí.

http://joluet.github.io/blog/2014/07/07/rxjava-retrofit/

  • KSOAP2 java.lang.RuntimeException: No se puede serializar
  • Cómo pasar String array a webservice usando ksoap2?
  • Cómo utilizar el tipo de llamada con EventBus
  • ¿Cómo puedo convertir los datos de InputStream en String en SOAP Webservices de Android
  • Mejor manera de crear un servicio Web en Android
  • Problema del API del cliente Jersey
  • No se pueden llamar métodos de servicio web desde el emulador de proyectos android
  • Llame al servicio web .NET en Android
  • Analizando la respuesta ksoap2
  • Respuesta de retorno de loopj asynchttpclient de Android
  • Android loopj + GCMIntentService enviar mensaje a un Handler en un hilo muerto
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.