Add Hydration Data

This section demonstrates adding hydration data. In the com.google.hydration data type, each data point represents the volume, in liters, of water consumed by a user as part of a single drink. Use a float to specify volume. The timestamp indicates when the drink was consumed.

Create a data source

Android

This example demonstrates using DataSource.Builder() to create a new data source named hydrationSource.

DataSource hydrationSource = new DataSource.Builder()
        .setDataType(DataType.TYPE_HYDRATION)
        ...
        .build();

REST

This example demonstrates calling the REST API to create a new data source named HydrationSource.

HTTP method

POST

Request URL

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

Request body

{
  "dataStreamName": "HydrationSource",
  "type": "raw",
  "application": {
    "detailsUrl": "http://example.com",
    "name": "My Example App",
    "version": "1"
  },
  "dataType": {
    "name": "com.google.hydration",
    "field": [
     {
      "name": "volume",
      "format": "floatPoint",
      "optional": false
     }
    ]
   }
}

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 -X POST \
  --header "Content-Type: application/json;encoding=utf-8" -d @hydration-ds.json \
  https://www.googleapis.com/fitness/v1/users/me/dataSources

Add Hydration Data

Android

This example demonstrates creating a new data point, and adding hydration data for a 0.3 liter drink of water, using the hydrationSource data source.

DataPoint hydration = DataPoint.create(hydrationSource);
hydration.setTimestamp(now.getMillis(), TimeUnit.MILLISECONDS);
hydration.getValue(FIELD_VOLUME).setFloatValues(0.3);

REST

HTTP method

PATCH

Request URL

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

Request body

{
 "minStartTimeNs": 1471644804000,
 "maxEndTimeNs": 1471644804001,
 "dataSourceId": "raw:com.google.hydration:407408718192:HydrationSource",
 "point": [
  {
   "startTimeNanos": 1471644804000,
   "endTimeNanos": 1471644804001,
   "dataTypeName": "com.google.hydration",
   "value": [
    {
     "fpVal": 0.3
    }
   ]
  }
 ]
}

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 PATCH \
  --header "Content-Type: application/json;encoding=utf-8" -d @nutrition-data.json \
  https://www.googleapis.com/fitness/v1/users/me/dataSources/raw:com.google.????????????????????????

Note the use of --request PATCH instead of -X POST.

发送以下问题的反馈:

此网页
Google Fit
Google Fit