我目前正在开发一个使用Google Fit API的Android应用。但是,当我从DataReadResult读取数据时,DataSet ds = result.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);得到:

IllegalArgumentException: Attempting to read data for com.google.step_count.delta, which was not requested


这是我的AsyncTask,它从以下位置获取DataReadResult:

public static class GetReadResultTask extends AsyncTask<Void, Void, DataReadResult> {

    protected DataReadResult doInBackground(Void... voids) {
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        long startTime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .bucketByTime(1, TimeUnit.HOURS)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();
        DataReadResult result =
                Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);
        return result;
    }
}


我怎样才能解决这个问题?任何帮助,将不胜感激。

最佳答案

我发现自己做错了。

聚合数据是在存储桶中返回的,而不是在数据集中返回的,因此,我不必执行result.getDataSets(DataType.TYPE_STEP_COUNT_DELTA);而不是调用List<Buckets> buckets = result.getBuckets(),然后遍历存储桶并使用以下方法获取数据集

buckets.get(currentIndex).getDataSet(DataType.AGGREGATE_STEP_COUNT_DELTA);

10-07 19:48
查看更多