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).
- Conectar con un emulador de Android a la localhost webservice
- Envío de imágenes de la galería al servicio web mediante SOAP
- Interacción entre Java y Android
- Java.net.UnknownHostException: No se puede resolver el host "<url>": ninguna dirección asociada con hostname y End of input en el carácter 0 de
- Enviar una imagen de Android a un servicio Web ASP.NET
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); }
- Envío de XML a un servicio web en Android
- KSoap2 Android recibe un arreglo de objetos
- HttpURLConnection getResponseCode () deos no devuelve si no hay conexión a Internet
- No se puede deserializar la instancia del objeto de START_ARRAY token en Spring Webservice
- Cómo manejar la conexión de red lenta en Java / Android
- Cómo sincronizar grandes listas entre cliente y servidor
- Crear webservice en drupal
- Analizador de XML común de Android
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í.
- Actualización de la ubicación de Android después de cada 5min Si el vehículo no se mueve
- Android – Parse JS generó urls con JSOUP