【问题标题】:Get data from onResponse callback and return in the same method从 onResponse 回调中获取数据并以相同的方法返回
【发布时间】:2016-09-29 11:11:56
【问题描述】:

这是我的代码。

private long getLastTimestamp()
{
    long timestamp = 0;
    retrofit2.Call<UserStatistic> call = api.getUserStatistics();

    call.enqueue(new Callback<UserStatistic>() {

        @Override
        public void onResponse(retrofit2.Call<UserStatistic> call, Response<UserStatistic> response) {
            if (response.body() != null)
            {
                List<Statistic> statistics = response.body().getData();

                statistics.get(statistics.size() - 1).Data;
            }
        }

        @Override
        public void onFailure(retrofit2.Call<UserStatistic> call, Throwable t) {
            Log.d("LOG", "Something went wrong :c");
        }

    });

    return timestamp;
}

而且我真的不知道如何简单地返回保存在时间戳变量中的响应。有任何想法吗?提前致谢。

【问题讨论】:

    标签: android callback retrofit


    【解决方案1】:

    如果要同步调用该方法,可以使用如下代码

    private long getLastTimestamp() {
    
            retrofit2.Call<UserStatistic> call = api.getUserStatistics();
    
            UserStatistic statistics = call.execute().body();
    
            long timestamp = statistics.YOUR_CODE;
    
            return timestamp;
        }
    

    或者如果你想进行异步调用,那么像这样调用这个方法

    private void callingMethod () {
    
            getLastTimestamp (new Callback<UserStatistic>() {
    
                @Override
                public void onResponse(retrofit2.Call<UserStatistic> call, Response<UserStatistic> response) {
                    if (response.body() != null)
                    {
                        List<Statistic> statistics = response.body().getData();
    
                        statistics.get(statistics.size() - 1).Data;
                    }
                }
    
                @Override
                public void onFailure(retrofit2.Call<UserStatistic> call, Throwable t) {
                    Log.d("LOG", "Something went wrong :c");
                }
    
            });
        }
    
        private long getLastTimestamp(Callback<UserStatistic> callback)
        {
            long timestamp = 0;
            retrofit2.Call<UserStatistic> call = api.getUserStatistics();
    
            call.enqueue(callback);
    
            return timestamp;
        }
    

    【讨论】:

    • 这是我需要的!谢谢!
    【解决方案2】:

    这是因为

    retrofit2.Call<UserStatistic> call = api.getUserStatistics();
    

    这一行

    call.enqueue(new Callback<UserStatistic>() {
    

    将创建一个侦听器,并且当在此侦听器的帮助内进行网络调用后加载数据时,改造将调用 onResponse() 方法

    【讨论】:

    • 是的,但我需要返回 response.body().getData();来自 main 方法,但我无法从我的回调 onresponse 中获取它。
    【解决方案3】:

    通过调用call.enqueue(...) 方法,网络操作在单独的线程上执行。

    如果你想在你的方法中返回它的结果,你必须使用call.execute() 方法来同步执行网络操作。

    但要小心,因为您无法在 UI 线程 (https://developer.android.com/training/basics/network-ops/connecting.html) 上执行网络操作。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 2017-07-26
      • 1970-01-01
      • 2019-06-16
      • 1970-01-01
      • 2016-06-04
      • 2011-09-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      相关资源
      最近更新 更多