AI-generated Key Takeaways
-
The aggregate daily nutrition data is read from a specific data source, resulting in a list of buckets, one per day, each containing a single dataset with nutrition data.
-
If no nutrition data exists for a time period, there is no dataset, and if multiple meal types are recorded, the value is set to UNKNOWN.
-
On Android, you can read this data by creating a data read request with specific aggregate data types.
-
Using REST, you can read the data by making a POST request to the specified URL with the appropriate request body.
-
A successful REST request returns a 200 OK status code and the response body contains a JSON representation of the data source.
This section demonstrates reading the aggregate daily nutrition from a specific
data source. The response contains a list of buckets (one per 24-hour period),
each with a single dataset and datapoint that contains a field for all recorded
nutrition data types. If no nutrition values exist for a particular time
period, there is no dataset. If more than one meal_type
is recorded, the
value will be set to UNKNOWN
.
Android
Your app can read the aggregate daily nutrition data for a user by creating a data read request, and specifying the aggregate for the 'DataType.TYPE_NUTRITION' and 'DataType.AGGREGATE_NUTRITION_SUMMARY' data types, as shown in the following example:
val readRequest = DataReadRequest.Builder()
.aggregate(DataType.AGGREGATE_NUTRITION_SUMMARY)
.bucketByTime(1, TimeUnit.DAYS)
.setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
.build()
REST
Your app can read the aggregate daily nutrition data for a user by
making a POST
request and querying the specified data source. You must
make a separate query for each data source.
HTTP method
POST
Request URL
https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
Request body
{
"aggregateBy": [{
"dataSourceId":
"raw:com.google.nutrition:407408718192:MyDataSource"
}],
"bucketByTime": { "durationMillis": 86400000 },
"startTimeMillis": 1471244400000,
"endTimeMillis": 1471259040000
}
Response
If the data source is created successfully, the response is a 200 OK
status code. The response body contains a JSON representation of the data
source, including a datasource.dataStreamId
property that you can use as
the data source ID for subsequent requests.
CURL command
$ 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