如何知道改装调用何时完成

时间:2016-01-12 19:59:05

标签: android web-services api retrofit retrofit2

有没有办法知道我的改装电话何时完成了它的职责?我喜欢知道什么时候收到所有数据,所以代码可以继续,比如开始另一个活动或者使用第一次调用的数据做一秒钟?

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


}

3 个答案:

答案 0 :(得分:3)

也许您可以使用两个布尔标志并在getContact方法之外启动新意图。

这样的事情:

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
       }

    }    
}

答案 1 :(得分:1)

移动

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

这样的callM onResponse方法内部。这样第一个callM将会执行,只要它完成callC就会执行,每当callC完成它就会抛出意图。

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

答案 2 :(得分:0)

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

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