添加营养数据

您可以向 Google 健身添加营养数据,只需创建数据源并使用 com.google.nutrition 数据类型。每个数据点都代表 膳食或零食中消耗的所有营养素的比例。此示例展示了如何将 营养数据。

创建数据源

Android

使用 DataSource.Builder 创建新的数据源。例如 nutritionSource

val nutritionSource = DataSource.Builder()
    .setDataType(DataType.TYPE_NUTRITION)
    // ...
    .build()

REST

调用 REST API 以创建新的数据源。例如 NutritionSource

HTTP 方法

POST

Request URL

https://www.googleapis.com/fitness/v1/users/me/dataSources

请求正文

{
  "dataStreamName": "NutritionSource",
  "type": "raw",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "My Example App",
    "version": "1"
  },
  "dataType": {
    "name": "com.google.nutrition",
   }
}

答案

如果数据源已成功创建,您会收到 200 OK HTTP 响应状态代码。响应正文包含 数据源,包括 datasource.dataStreamId 属性。使用此 ID 用作 dataSourceId 以添加数据。

C网址 命令

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

添加营养数据

Android

此示例展示了如何创建新数据点和添加营养成分 香蕉的数据,使用 nutritionSource 数据源。

val nutrients = mapOf(
    Field.NUTRIENT_TOTAL_FAT to 0.4f,
    Field.NUTRIENT_SODIUM to 1f,
    Field.NUTRIENT_SATURATED_FAT to 0.1f,
    Field.NUTRIENT_PROTEIN to 1.3f,
    Field.NUTRIENT_TOTAL_CARBS to 27.0f,
    Field.NUTRIENT_CHOLESTEROL to 0.0f,
    Field.NUTRIENT_CALORIES to 105.0f,
    Field.NUTRIENT_SUGAR to 14.0f,
    Field.NUTRIENT_DIETARY_FIBER to 3.1f,
    Field.NUTRIENT_POTASSIUM to 422f
)
val banana = DataPoint.builder(nutritionSource)
    .setTimestamp(timestamp, TimeUnit.MILLISECONDS)
    .setField(Field.FIELD_FOOD_ITEM, "banana")
    .setField(Field.FIELD_MEAL_TYPE, Field.MEAL_TYPE_SNACK)
    .setField(Field.FIELD_NUTRIENTS, nutrients)
    .build()

REST

此示例展示了如何使用 NutritionSource 数据源。营养数据类型的值是 营养成分(地图)、膳食类型(4 = “零食”)和实际的菜品 (字符串)。

HTTP 方法

PATCH

Request URL

  https://www.googleapis.com/fitness/v1/users/me/dataSources/datasource.dataStreamId/datasets/1574159699023000000-1574159699023999000
 

请求正文

    {
      "minStartTimeNs": 1574159699023000000,
      "maxEndTimeNs": 1574159699023999000,
      "dataSourceId": "datasource.dataStreamId",
      "point": [
        {
          "startTimeNanos": 1574159699023000000,
          "endTimeNanos": 1574159699023999000,
          "dataTypeName": "com.google.nutrition",
          "value": [
            {
              "mapVal": [
              {
                "key": "fat.total",
                "value": {
                  "fpVal": 0.4
                }
              },
              {
                "key": "sodium",
                "value": {
                  "fpVal": 1.0
                }
              },
              {
                "key": "fat.saturated",
                "value": {
                  "fpVal": 0.1
                }
              },
              {
                "key": "protein",
                "value": {
                  "fpVal": 1.3
                }
              },
              {
                "key": "carbs.total",
                "value": {
                  "fpVal": 27.0
                }
              },
              {
                "key": "cholesterol",
                "value": {
                  "fpVal": 0.0
                }
              },
              {
                "key": "calories",
                "value": {
                  "fpVal": 105.0
                }
              },
              {
                "key": "sugar",
                "value": {
                  "fpVal": 14.0
                }
              },
              {
                "key": "dietary_fiber",
                "value": {
                  "fpVal": 3.1
                }
              },
              {
                "key": "potassium",
                "value": {
                  "fpVal": 422.0
                }
              }
             ]
            },
            {
              "intVal": 4
            },
            {
              "strVal": "banana"
            }
          ]
        }
      ]
    }

答案

如果数据点已成功创建,您会收到 200 OK HTTP 响应状态代码。响应正文包含 数据集。

C网址 命令

$ curl --header "Authorization: Bearer ya29.yourtokenvalue --request PATCH \
--header "Content-Type: application/json;encoding=utf-8" --data @nutrition-data.json \
https://www.googleapis.com/fitness/v1/users/me/dataSources/datasource.dataStreamId/datasets/1574159699023000000-1574159699023999000