8

有没有办法知道我的改造电话何时完成它的职责?我想知道何时收到所有数据,以便代码可以继续进行,例如开始另一个活动或使用第一次调用中的数据进行第二次?

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


}
4

4 回答 4

5

也许您可以使用两个布尔标志并在 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
       }

    }    
}
于 2016-01-12T20:37:45.460 回答
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());
    }
});

insidecallMonResponse方法是这样的。这样一来,冷杉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());
        }
    });    
  }
于 2016-01-12T20:40:45.653 回答
0

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

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

于 2016-06-10T08:06:29.973 回答
0

如果您想在完成改造调用后在另一个活动或另一个片段中执行某些任务但您的活动已更改并且您仍在等待改造调用完成,您可以使用接口发送数据。

假设您有一个通过改造调用 API 的 Activity A,而此调用正在执行时,用户已移动到另一个 Activtiy B,现在您需要在 Activity B 中调用一个方法(或执行其他操作)完成来自 A 的电话。

在 A 中创建一个接口 AB,实现 AB 并在 Activity B 中覆盖其方法,并通过 API 调用的 onResponse() 方法中的 Activity A 中的接口 AB 的对象调用该方法。

于 2021-10-04T15:21:21.627 回答