是否有延迟阅读营养数据?我插入营养数据,我试图读取它们,但DataPoint的大小仍然等于0 ...
我插入了这样的数据:
long now = System.currentTimeMillis();
DataSource nutritionSource = new DataSource.Builder()
.setAppPackageName(getApplicationContext().getPackageName())
.setType(DataSource.TYPE_RAW)
.setDataType(DataType.TYPE_NUTRITION)
.build();
DataSet dataSet = DataSet.create(nutritionSource);
DataPoint dataPoint = DataPoint.create(nutritionSource);
dataPoint.setTimestamp(now, TimeUnit.MILLISECONDS);
dataPoint.getValue(Field.FIELD_FOOD_ITEM).setString(name_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_CALORIES, calorie_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_SUGAR,sugar_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_TOTAL_FAT,fat_food);
dataPoint.getValue(Field.FIELD_NUTRIENTS).setKeyValue(Field.NUTRIENT_PROTEIN,protein_food);
dataPoint.getValue(Field.FIELD_MEAL_TYPE).setInt(Field.MEAL_TYPE_UNKNOWN);
dataSet.add(dataPoint);
然后,我试图像这样阅读它们:
DataReadRequest readRequest = new DataReadRequest.Builder()
.aggregate(DataType.TYPE_NUTRITION, DataType.AGGREGATE_NUTRITION_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build();
// Invoke the History API to fetch the data with the query and await the result of
// the read request.
DataReadResult dataReadResult =
Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);
if(dataReadResult.getStatus().isSuccess()){
Log.i("TAG","isSuccess to read nutrition data");
if(dataReadResult.getDataSet(DataType.TYPE_NUTRITION).getDataPoints().size() > 0){
Log.i("TAG","calorie : "+dataReadResult.getDataSet(DataType.TYPE_NUTRITION).getDataPoints().get(0).getValue(Field.FIELD_CALORIES));
}
}
我想获得当天的所有数据点并与他们一起工作,但我不知道怎么做?
谢谢,
卡迈勒莫尔
答案 0 :(得分:1)
不确定这是否仍然是一个问题,但尝试使用它来获取每个"桶"汇总数据。
List<Bucket> buckets = dataReadResult.getBuckets();
for (int a = buckets.size() - 1; a > -1; a--) {
Bucket bucket = buckets.get(a);
for (DataSet dataSet : bucket.getDataSets()) {
if(dataSet.getDataType().getName().equals(DataType.TYPE_NUTRITION.getName())){
for (DataPoint dp : dataSet.getDataPoints()) {
Log.i(TAG, "Data point:");
Log.i(TAG, "\tType: " + dp.getDataType().getName());
for(Field field : dp.getDataType().getFields()) {
Log.i(TAG, "\tField: " + field.getName() +
" Value: " + dp.getValue(field));
}
}
}
}
}