检索Google适合步骤时出错

时间:2014-11-20 21:50:29

标签: google-fit google-fit-sdk

我正在使用此代码尝试检索过去14小时内完成的步骤。

YApp myApp = (mYApp) ctx;
        mGoogleApiClient = myApp.getMyUser();
        mGoogleApiClient.reconnect();


        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.HOUR_OF_DAY, -16);
        long startTime = cal.getTimeInMillis();

        PendingResult<DataReadResult> pendingResult =
                Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
                                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                                .bucketByTime(1, TimeUnit.DAYS)
                                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                                .build());

        DataReadResult dataReadResult = pendingResult.await();



        DataSet ds = dataReadResult.getDataSet(DataType.AGGREGATE_STEP_COUNT_DELTA);

我的datareadresult返回成功,但当我尝试将结果读入数据集时,我收到此错误

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

我尝试过各种各样的DataTypes但每次都收到同样的错误,我做错了什么?!

2 个答案:

答案 0 :(得分:8)

对于其他任何对此感到沮丧的人:)

mYApp myApp = (mYApp) ctx;
        mGoogleApiClient = myApp.getMyUser();
        mGoogleApiClient.reconnect();


        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.HOUR_OF_DAY, -12);
        long startTime = cal.getTimeInMillis();

        PendingResult<DataReadResult> pendingResult =
                Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
                                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                                .bucketByTime(1, TimeUnit.DAYS)
                                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                                .build());

        DataReadResult dataReadResult = pendingResult.await();



        if (dataReadResult.getBuckets().size() > 0) {
            Log.i(TAG, "Number of returned buckets of DataSets is: "
                    + dataReadResult.getBuckets().size());
            for (Bucket bucket : dataReadResult.getBuckets()) {
                List<DataSet> dataSets = bucket.getDataSets();
                for (DataSet dataSet : dataSets) {
                    Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
                    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

                    for (DataPoint dp : dataSet.getDataPoints()) {
                        Log.i(TAG, "Data point:");
                        Log.i(TAG, "\tType: " + dp.getDataType().getName());
                        Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                        Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
                        for(Field field : dp.getDataType().getFields()) {
                            Log.i(TAG, "\tField: " + field.getName() +
                                    " Value: " + dp.getValue(field));
                        }
                    }
                }
            }
        } else if (dataReadResult.getDataSets().size() > 0) {
            Log.i(TAG, "Number of returned DataSets is: "
                    + dataReadResult.getDataSets().size());
            for (DataSet dataSet : dataReadResult.getDataSets()) {
                Log.i(TAG, "Data returned for Data type: " + dataSet.getDataType().getName());
                SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

                for (DataPoint dp : dataSet.getDataPoints()) {
                    Log.i(TAG, "Data point:");
                    Log.i(TAG, "\tType: " + dp.getDataType().getName());
                    Log.i(TAG, "\tStart: " + dateFormat.format(dp.getStartTime(TimeUnit.MILLISECONDS)));
                    Log.i(TAG, "\tEnd: " + dateFormat.format(dp.getEndTime(TimeUnit.MILLISECONDS)));
                    for (Field field : dp.getDataType().getFields()) {
                        Log.i(TAG, "\tField: " + field.getName() +
                                " Value: " + dp.getValue(field));
                    }
                }
            }
        }

答案 1 :(得分:4)

只是为了澄清

PendingResult<DataReadResult> pendingResult =
            Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
                            .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                            .bucketByTime(1, TimeUnit.DAYS)
                            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                            .build());

返回带有列表列表的DataReadResult。

PendingResult<DataReadResult> pendingResult =
            Fitness.HistoryApi.readData(mGoogleApiClient, new DataReadRequest.Builder()
                            .read(DataType.TYPE_STEP_COUNT_DELTA)
                            .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                            .build());

返回带有可以使用

获取的DataSet的DataReadResult
dataReadResult.getDataSet(DataType.TYPE_STEP_COUNT_DELTA);