如何知道改造呼叫何时完成

有没有办法知道我的改造呼叫何时完成它的责任? 我喜欢知道何时收到所有数据,以便代码可以继续前进,如启动另一个活动或使用第一次调用的数据创建第二个活动?

Ps .:我正在使用异步请求(.enqueue)。

编辑:

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


}

也许你可以使用两个布尔标志并在你的getContact方法之外启动新的intent。

像这样的东西:

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
       }

    }    
}

移动

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

里面callMonResponse方法就是这样。 这种方式callM将执行,只要它完成, callC将执行,并且每当callC结束时它将抛出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());
        }
    });    
  }

如果你有任何请求相互依赖,这将是复杂的。 你可以用RxJava通过使用flatMap方法来链接多个请求并避免回调地狱。 这很简单。 你可以在这里了解它。

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

链接地址: http://www.djcxy.com/p/89703.html

上一篇: How to know when a Retrofit call is finished

下一篇: Detect file type on dragenter cross browser solution