如何知道 Retrofit 调用何时完成

8

有没有办法知道我的Retrofit请求何时完成?我想知道何时接收到所有数据,以便代码可以继续进行,例如启动另一个活动或使用第一个调用的数据来进行第二个调用。

附:我正在使用异步请求(.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);


}

我在Google上搜索了一下,但没有找到代码示例,您知道哪里可以找到吗? - Rafael
1
为什么不把你的尝试发出来呢? - Blackbelt
发布了,感谢回答。 - Rafael
你想在开始新活动之前等待两个调用都返回吗? - Blackbelt
是的,那就可以了。 - Rafael
显示剩余7条评论
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
       }

    }    
}

1
使用这种方法时,当代码到达startIntent()并且尚未收到响应时,它会停在那里,我需要它具有响应性,就像当变量设置为true时,它会再次进行测试... 有什么办法可以做到这一点吗? - Rafael

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

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

我有点想避免进入回调地狱,因为后面我还要使用8个不同的调用来做同样的事情,把一个调用设置在另一个调用内部会变得有点混乱... 我觉得。 - Rafael

0

4
参考链接已损坏。 - iSrinivasan27

0
如果您想在Retrofit调用完成后在另一个活动或片段中执行某些任务,但您的活动已更改并且仍在等待Retrofit调用完成,您可以使用接口来发送数据。
假设您有一个Activity A正在通过Retrofit调用API,而此调用正在执行时用户已移动到另一个Activity B,现在您需要在A完成调用后在Activity B中调用方法(或执行其他操作)。
在A中创建一个接口AB,在A中实现AB并在Activity B中覆盖其方法,并通过接口AB的对象在API调用的onResponse()方法中在Activity A中调用该方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接