Q:

Get data from onResponse callback and return in the same method

It's my code.
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;
}

And i really don't know how to simply return the response saved in time-stamp variable. Any ideas? Thanks in advance. 

A:

if you want to call this method synchronously, you can use the following code
private long getLastTimestamp() {

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

        UserStatistic statistics = call.execute().body();

        long timestamp = statistics.YOUR_CODE;

        return timestamp;
    }

or if you want to make asynchronous call, then call this method like this
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;
    }

