【问题标题】:How to know when a Retrofit call is finished如何知道 Retrofit 调用何时结束
【发布时间】:2016-04-17 14:27:09
【问题描述】:

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

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


}

【问题讨论】:

  • 你可以提供一个委托来排队(又名监听器)。
  • 我在哪里可以找到代码示例,我已经使用它进行了搜索,但一无所获......
  • 你为什么不发布你的尝试?
  • 已发布,感谢您的回答
  • 您要等待两个调用都返回,然后再开始新活动?

标签: android web-services api retrofit retrofit2


【解决方案1】:

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

    }    
}

【讨论】:

  • 在代码到达 startIntent() 并且尚未收到响应时使用此方法,它会停在那里,我需要它响应,例如当变量设置为 true 时,它再次进行测试...有什么办法吗?
【解决方案2】:

移动

//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 个不同的调用来做同样的事情,并且将一个设置在另一个内部会有点混乱......我认为
【解决方案3】:

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

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

【讨论】:

  • 参考链接已损坏
【解决方案4】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-06-07
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多