2016-01-12 19 views
6

Có cách nào để biết khi nào cuộc gọi Retrofit của tôi đã hoàn thành nhiệm vụ của nó? Tôi thích biết khi nào tất cả dữ liệu được nhận để mã có thể di chuyển, như bắt đầu một hoạt động khác hoặc sử dụng dữ liệu từ cuộc gọi đầu tiên để thực hiện một giây?Làm thế nào để biết khi nào một cuộc gọi Retrofit được hoàn thành

Ps .: Tôi đang sử dụng yêu cầu không đồng bộ (.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

bạn có thể cung cấp cho đại biểu để enqueue (aka người nghe). – Blackbelt

+0

Tôi có thể tìm thấy mẫu mã ở đâu, tôi đã goggled nó và không tìm thấy gì ... – Rafael

+1

tại sao bạn không đăng nỗ lực của mình? – Blackbelt

Trả lời

3

lẽ bạn có thể sử dụng hai lá cờ boolean và bắt đầu mục đích mới bên ngoài của phương pháp getContact của bạn.

Something như thế này:

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

Sử dụng phương pháp này khi mã đạt đến startIntent() và các phản hồi chưa được nhận, nó sẽ dừng lại ở đó, tôi cần nó đáp ứng, như khi các biến được đặt thành true, nó làm cho kiểm tra lại ... Bất kỳ cách nào để làm điều đó? – Rafael

1

Move

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

bên onResponse phương pháp callM 's như thế này. Bằng cách đó, linh kiện callM sẽ thực thi, bất cứ khi nào nó kết thúc callC sẽ thực thi và bất cứ khi nào callC hoàn tất, nó sẽ ném Mục đích.

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

Tôi đang cố gắng không để có được trong một địa ngục gọi lại, bởi vì sau này tôi phải làm điều tương tự bằng cách sử dụng 8 khác nhau ent cuộc gọi, và thiết lập một bên trong khác sẽ nhận được kinda lộn xộn ... Tôi nghĩ rằng – Rafael

0

Nếu bạn có bất kỳ yêu cầu nào phụ thuộc lẫn nhau, nó sẽ phức tạp. Bạn có thể làm điều đó với RxJava bằng cách sử dụng phương thức flatMap để yêu cầu nhiều chuỗi và tránh địa chỉ gọi lại. nó khá đơn giản. Bạn có thể tìm hiểu nó ở đây.

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

+1

Tham khảo Liên kết bị hỏng – JUL2791

Các vấn đề liên quan