2016-01-12 3 views
6

Gibt es eine Möglichkeit zu wissen, wann mein Retrofit-Anruf beendet ist? Ich mag wissen, wenn alle Daten empfangen werden, so dass der Code weitergehen kann, wie eine andere Aktivität zu starten oder Daten aus dem ersten Anruf zu verwenden, um eine Sekunde zu machen?Wissen, wann ein Nachrüstanruf beendet ist

Ps .: Ich verwende eine asynchrone Anfrage (.enqueue).

Edit:

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); 


} 
+0

Sie einen Delegaten (auch bekannt als Zuhörer) einzureihen zur Verfügung stellen kann. – Blackbelt

+0

Wo kann ich ein Codebeispiel finden? Ich habe es geglotzt und nichts gefunden ... – Rafael

+1

Warum posten Sie Ihren Versuch nicht? – Blackbelt

Antwort

3

Vielleicht können Sie zwei Boolesche Flags verwenden und die neue Absicht außerhalb Ihres getContact Methode starten.

Etwas wie folgt aus:

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 
     } 

    }  
} 
+0

Wenn diese Methode verwendet wird, wenn der Code den startIntent() erreicht und die Antworten noch nicht empfangen wurden, wird es dort anhalten, ich brauche es, um darauf zu reagieren, wie wenn die Variablen auf true gesetzt sind ... Irgendeine Möglichkeit es zu tun? – Rafael

1

Verschieben

//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()); 
    } 
}); 

innerhalb callM ‚s onResponse Methode wie folgt. Auf diese Weise werden die Fires callM ausgeführt, wann immer es fertig ist callC wird ausgeführt, und wann immer callC fertig ist, wird es den Intent werfen.

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()); 
     } 
    });  
    } 
+0

Ich versuche irgendwie, nicht in eine Callback Hell zu kommen, denn später muss ich das gleiche tun mit 8 verschiedenen ent calls, und das Setzen in einem anderen wird irgendwie unordentlich ... Ich denke, – Rafael

0

Wenn Sie eine Anfrage voneinander abhängig haben, wird es komplex sein. Sie können das mit RxJava tun, indem Sie die flatMap-Methode verwenden, um mehrere Anfragen zu verketten und die Callback-Hölle zu vermeiden. es ist ziemlich einfach. Du kannst es hier lernen.

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

+1

Reference Link ist kaputt – JUL2791