讀取每日營養匯總資料

本節示範如何從特定資料來源讀取每日營養匯總資料。回應會包含一連串的 bucket (每 24 小時一個),每個 bucket 都有一個資料集和資料點,內含所有記錄的營養資料類型欄位。如果特定時間範圍沒有營養價值,就不會有資料集。如果記錄多個 meal_type,值會設為 UNKNOWN

Android

您的應用程式可以建立資料讀取要求,並指定 'DataType.TYPE_NUTRITION''DataType.AGGREGATE_NUTRITION_SUMMARY' 資料類型的匯總,讀取使用者的每日營養匯總資料,如下列範例所示:

val readRequest = DataReadRequest.Builder()
    .aggregate(DataType.AGGREGATE_NUTRITION_SUMMARY)
    .bucketByTime(1, TimeUnit.DAYS)
    .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
    .build()

REST

應用程式可以發出 POST 要求並查詢指定資料來源,讀取使用者的每日營養總資料。您必須為每個資料來源分別提出查詢。

HTTP 方法

POST

要求網址

https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

要求主體

{
  "aggregateBy": [{
    "dataSourceId":
      "raw:com.google.nutrition:407408718192:MyDataSource"
  }],
  "bucketByTime": { "durationMillis": 86400000 },
  "startTimeMillis": 1471244400000,
  "endTimeMillis": 1471259040000
}

回應

如果資料來源建立成功,回應會是 200 OK 狀態碼。回應主體包含資料來源的 JSON 表示法,包括可用於後續要求的 datasource.dataStreamId 屬性 (資料來源 ID)。

CURL 指令

$ curl --header "Authorization: Bearer ya29.yourtokenvalue --request POST \
--header "Content-Type: application/json;encoding=utf-8" --data @aggregate.json \
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate