strava api v3-“获取所有运动员活动”-如何计算某个时间点的“之前”和“之后”?

hsgswve4  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(348)

我正在编写一个java程序来获取从2020年1月1日到当前日期的所有活动。
我的问题是strava需要一个整数,如下图所示,用于请求的'before'和'after'部分。由于在他们的网站上没有提供关于这两个变量的价值是基于什么的进一步信息,我不知道该放在那里。
下面是我提到的StravaAPI的描述图片,以及它的链接:
https://developers.strava.com/docs/reference/#api-活动getloggedinathleteactivities
这是我的工作代码,它只需要正确的值:

public List<AthleteActivities> getAthleteActivities(long before, long after, int page, int per_page,
            String access_token) {
        System.out.println(access_token);
        List<AthleteActivities> aa = new ArrayList<AthleteActivities>();
        Type listType = new TypeToken<List<AthleteActivities>>() {
        }.getType();

        try {
            while (true) {
                String link = "https://www.strava.com/api/v3/athlete/activities?before=" + before + "&after=" + after
                        + "&page=" + page + "&per_page=" + per_page;
                URL url = new URL(link);

                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("GET");
                con.setRequestProperty("Content-Type", "application/json; utf-8");
                con.setRequestProperty("Accept", "application/json");
                con.setRequestProperty("Authorization", "Bearer " + access_token);
                con.setDoOutput(true);

                Reader reader = new InputStreamReader(con.getInputStream(), "UTF-8");
                Gson gson = new Gson();

                List<AthleteActivities> temp = gson.fromJson(reader, listType);
                if (temp.size() == 0) {
                    return aa;
                }

                aa.addAll(temp);
                page++;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        // return null;

    }

我使用gson保存从这个类的查询中得到的返回的.json:

import com.google.gson.annotations.SerializedName;

public class AthleteActivities {
    @SerializedName("resource_state")
    private double resource_state;
    @SerializedName("name")
    private String name;
    @SerializedName("distance")
    private double distance;
    @SerializedName("moving_time")
    private double moving_time;
    @SerializedName("elapsed_time")
    private double elapsed_time;
    @SerializedName("total_elevation_gain")
    private double total_elevation_gain;
    @SerializedName("type")
    private String type;
    @SerializedName("id")
    private double id;
    @SerializedName("external_id")
    private String external_id;
    @SerializedName("upload_id")
    private double upload_id;
    @SerializedName("start_date")
    private String start_date;
    @SerializedName("start_date_local")
    private String start_date_local;
    @SerializedName("timezone")
    private String timezone;
    @SerializedName("utc_offset")
    private double utc_offset;
    @SerializedName("average_speed")
    private double average_speed;
    @SerializedName("max_speed")
    private double max_speed;
    @SerializedName("average_cadence")
    private double average_cadence;
    @SerializedName("average_watts")
    private double average_watts;
    @SerializedName("weighted_average_watts")
    private double weighted_average_watts;
    @SerializedName("kilojoules")
    private double kilojoules;
    @SerializedName("device_watts")
    private boolean device_watts;
    @SerializedName("has_heartrate")
    private boolean has_heartrate;
    @SerializedName("average_heartrate")
    private double average_heartrate;
    @SerializedName("max_heartrate")
    private double max_heartrate;
    @SerializedName("max_watts")
    private double max_watts;
    @SerializedName("pr_count")
    private double pr_count;
    @SerializedName("total_photo_count")
    private double total_photo_count;
    @SerializedName("elev_high")
    private double elev_high;
    @SerializedName("elev_low")
    private double elev_low;
    @SerializedName("has_kudoed")
    private boolean has_kudoed;
    @SerializedName("suffer_score")
    private double suffer_score;

    public double getResource_state() {
        return resource_state;
    }

    public String getName() {
        return name;
    }

    public double getDistance() {
        return distance;
    }

    public double getMoving_time() {
        return moving_time;
    }

    public double getElapsed_time() {
        return elapsed_time;
    }

    public double getTotal_elevation_gain() {
        return total_elevation_gain;
    }

    public String getType() {
        return type;
    }

    public double getId() {
        return id;
    }

    public String getExternal_id() {
        return external_id;
    }

    public double getUpload_id() {
        return upload_id;
    }

    public String getStart_date() {
        return start_date;
    }

    public String getStart_date_local() {
        return start_date_local;
    }

    public String getTimezone() {
        return timezone;
    }

    public double getUtc_offset() {
        return utc_offset;
    }

    public double getAverage_speed() {
        return average_speed;
    }

    public double getMax_speed() {
        return max_speed;
    }

    public double getAverage_cadence() {
        return average_cadence;
    }

    public double getAverage_watts() {
        return average_watts;
    }

    public double getWeighted_average_watts() {
        return weighted_average_watts;
    }

    public double getKilojoules() {
        return kilojoules;
    }

    public boolean isDevice_watts() {
        return device_watts;
    }

    public boolean isHas_heartrate() {
        return has_heartrate;
    }

    public double getAverage_heartrate() {
        return average_heartrate;
    }

    public double getMax_heartrate() {
        return max_heartrate;
    }

    public double getMax_watts() {
        return max_watts;
    }

    public double getPr_count() {
        return pr_count;
    }

    public double getTotal_photo_count() {
        return total_photo_count;
    }

    public double getElev_high() {
        return elev_high;
    }

    public double getElev_low() {
        return elev_low;
    }

    public boolean isHas_kudoed() {
        return has_kudoed;
    }

    public double getSuffer_score() {
        return suffer_score;
    }

}

我真的很感谢任何帮助,我可以如何计算这些整数值来代表我的请求所需的时间。
如果你需要更多的信息,请让我通过评论知道。

piv4azn7

piv4azn71#

如api文件所述, before 以及 after 是历元时间戳(自1970年1月1日以来的秒数,表示时间点的常用方式)。
您可以使用epoch转换器将人类可读的时间戳转换为epoch时间戳,或者最好使用java日期/时间类:

//1 January 2020 00:00:00 UTC
OffsetDateTime after = OffsetDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
getAthleteActivities(..., after.toEpochSecond(), ...);

如果您需要本地时区而不是utc:

//1 January 2020 00:00:00 CET
ZonedDateTime after = ZonedDateTime.of(2020, 1, 1, 0, 0, 0, 0, ZoneId.of("Europe/Vienna"));
getAthleteActivities(..., after.toEpochSecond(), ...);

相关问题