读取每日营养汇总数据
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本部分演示如何读取特定食材
数据源。响应包含一个存储分区列表(每 24 小时一个存储分区)、
每个数据集都有一个数据集和数据点,其中包含一个涵盖所有已记录数据的字段
营养数据类型。如果在某特定时间无法测出营养成分值,
则目前还没有数据集。如果录制了多个 meal_type
,
值将设置为 UNKNOWN
。
REST
您的应用可以通过以下方式读取用户的每日汇总营养数据:
发出 POST
请求并查询指定的数据源。您必须
您可以为每个数据源创建单独的查询
HTTP 方法
POST
Request URL
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。
C网址 命令
$ 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
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis documentation explains how to access daily aggregate nutrition data from a specific data source.\u003c/p\u003e\n"],["\u003cp\u003eThe data is organized in 24-hour buckets, with each bucket containing a single dataset and datapoint for all recorded nutrition data types.\u003c/p\u003e\n"],["\u003cp\u003eAndroid and REST API examples are provided for retrieving this data.\u003c/p\u003e\n"],["\u003cp\u003eIf multiple meal types are logged, the \u003ccode\u003emeal_type\u003c/code\u003e field will be set to \u003ccode\u003eUNKNOWN\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eMissing nutrition values for a given time period result in an empty dataset for that period.\u003c/p\u003e\n"]]],[],null,["# Read Aggregate Daily Nutrition Data\n\nThis section demonstrates reading the aggregate daily nutrition from a specific\ndata source. The response contains a list of buckets (one per 24-hour period),\neach with a single dataset and datapoint that contains a field for all recorded\nnutrition data types. If no nutrition values exist for a particular time\nperiod, there is no dataset. If more than one `meal_type` is recorded, the\nvalue will be set to `UNKNOWN`. \n\n### Android\n\nYour app can read the aggregate daily nutrition data for a user by creating\na data read request, and specifying the aggregate for the\n['DataType.TYPE_NUTRITION'](/android/reference/com/google/android/gms/fitness/data/DataType#TYPE_NUTRITION) and\n['DataType.AGGREGATE_NUTRITION_SUMMARY'](/android/reference/com/google/android/gms/fitness/data/DataType#AGGREGATE_NUTRITION_SUMMARY) data types, as shown in the\nfollowing example: \n\n val readRequest = DataReadRequest.Builder()\n .aggregate(DataType.AGGREGATE_NUTRITION_SUMMARY)\n .bucketByTime(1, TimeUnit.DAYS)\n .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)\n .build()\n\n### REST\n\nYour app can read the aggregate daily nutrition data for a user by\nmaking a `POST` request and querying the specified data source. You must\nmake a separate query for each data source.\n\n**HTTP method** \n\n POST\n\n**Request URL** \n\n https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate\n\n**Request body** \n\n {\n \"aggregateBy\": [{\n \"dataSourceId\":\n \"raw:com.google.nutrition:407408718192:MyDataSource\"\n }],\n \"bucketByTime\": { \"durationMillis\": 86400000 },\n \"startTimeMillis\": 1471244400000,\n \"endTimeMillis\": 1471259040000\n }\n\n**Response**\n\nIf the data source is created successfully, the response is a `200 OK`\nstatus code. The response body contains a JSON representation of the data\nsource, including a `datasource.dataStreamId` property that you can use as\nthe data source ID for subsequent requests.\n\n**CURL command** \n\n $ curl --header \"Authorization: Bearer ya29.yourtokenvalue --request POST \\\n --header \"Content-Type: application/json;encoding=utf-8\" --data @aggregate.json \\\n https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"]]