Các API của Google Fit, bao gồm cả API Google Fit REST, sẽ ngừng hoạt động vào năm 2026. Kể từ ngày 1 tháng 5 năm 2024, nhà phát triển không thể đăng ký sử dụng các API này.
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể thêm dữ liệu về lượng nước vào Google Fit bằng cách tạo nguồn dữ liệu và sử dụng
loại dữ liệu com.google.hydration. Mỗi điểm dữ liệu đại diện cho khối lượng,
tính bằng lít mà người dùng tiêu thụ trong một ly đồ uống. Sử dụng số thực dấu phẩy động để
chỉ định âm lượng.
Lưu ý: Dấu thời gian cho biết thời điểm đồ uống được tiêu thụ. Vì com.google.hydration là loại dữ liệu tức thì, nên thời gian bắt đầu và thời gian kết thúc phải giống nhau.
Tạo nguồn dữ liệu
Android
Sử dụng DataSource.Builder để tạo nguồn dữ liệu mới. Ví dụ: hydrationSource.
Nếu nguồn dữ liệu được tạo thành công, thì phản hồi sẽ có trạng thái 200 OK
. Nội dung phản hồi chứa bản trình bày JSON của nguồn dữ liệu,
bao gồm thuộc tính datasource.dataStreamId mà bạn có thể sử dụng làm dữ liệu
mã nguồn cho các yêu cầu tiếp theo.
Nếu điểm dữ liệu được tạo thành công, bạn sẽ nhận được một HTTP 200 OK
mã trạng thái phản hồi. Nội dung phản hồi chứa bản trình bày JSON của
tập dữ liệu.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eYou can add hydration data to Google Fit by creating a data source and using the \u003ccode\u003ecom.google.hydration\u003c/code\u003e data type.\u003c/p\u003e\n"],["\u003cp\u003eEach data point represents the volume of a single drink consumed by a user, specified in liters using a float value, with the timestamp indicating the consumption time.\u003c/p\u003e\n"],["\u003cp\u003eData sources can be created using the \u003ccode\u003eDataSource.Builder\u003c/code\u003e in Android or by calling the REST API with a specific request body.\u003c/p\u003e\n"],["\u003cp\u003eHydration data can be added using \u003ccode\u003eDataPoint.builder\u003c/code\u003e in Android or through the REST API with a PATCH request containing the data point details.\u003c/p\u003e\n"]]],[],null,["# Add Hydration Data\n\nYou can add hydration data to Google Fit by [creating a data source](#creating_a_data_source) and using\nthe [`com.google.hydration`](/android/reference/com/google/android/gms/fitness/data/DataType#TYPE_HYDRATION) data type. Each data point represents the volume,\nin liters, consumed by a user as part of a single drink. Use a float to\nspecify volume.\nNote: The timestamp indicates when the drink was consumed. Because [`com.google.hydration`](/android/reference/com/google/android/gms/fitness/data/DataType#TYPE_HYDRATION) is an instantaneous data type, the start and end time should be the same.\n\nCreating a data source\n----------------------\n\n### Android\n\nUse `DataSource.Builder` to create a new data source. For example, `hydrationSource`. \n\n val hydrationSource = DataSource.Builder()\n .setDataType(DataType.TYPE_HYDRATION)\n .setStreamName(\"hydrationSource\")\n // ... \n .build()\n\n### REST\n\nCall the REST API to create a new data source. For example, `HydrationSource`.\n\n**HTTP method** \n\n POST\n\n**Request URL** \n\n https://www.googleapis.com/fitness/v1/users/me/dataSources\n\n**Request body** \n\n {\n \"dataStreamName\": \"HydrationSource\",\n \"type\": \"raw\",\n \"application\": {\n \"detailsUrl\": \"http://example.com\",\n \"name\": \"My Example App\",\n \"version\": \"1\"\n },\n \"dataType\": {\n \"name\": \"com.google.hydration\",\n \"field\": [\n {\n \"name\": \"volume\",\n \"format\": \"floatPoint\",\n \"optional\": false\n }\n ]\n }\n }\n\n**Response**\n\nIf the data source is created successfully, the response is a `200 OK` status\ncode. The response body contains a JSON representation of the data source,\nincluding a `datasource.dataStreamId` property that you can use as the data\nsource 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 @hydration-ds.json \\\n https://www.googleapis.com/fitness/v1/users/me/dataSources\n\nAdding Hydration Data\n---------------------\n\n### Android\n\nThis example shows you how to create a new data point, and add hydration\ndata for a 0.3 liter drink of water, using your data source. \n\n val hydration = DataPoint.builder(hydrationSource)\n .setTimestamp(timestamp, TimeUnit.MILLISECONDS)\n .setField(FIELD_VOLUME, 0.3f)\n .build()\n\n### REST\n\nThis example shows you how to add hydration data using your data\nsource.\n\n**HTTP method** \n\n PATCH\n\n**Request URL** \n\n https://www.googleapis.com/fitness/v1/users/me/dataSources/raw:com.google.hydration:407408718192:HydrationSource/datasets/1275753581000000000-1275753581000000000\n\n**Request body** \n\n {\n \"minStartTimeNs\": 1275753581000000000,\n \"maxEndTimeNs\": 1275753581000000000,\n \"dataSourceId\": \"raw:com.google.hydration:407408718192:HydrationSource\",\n \"point\": [\n {\n \"startTimeNanos\": 1275753581000000000,\n \"endTimeNanos\": 1275753581000000000,\n \"dataTypeName\": \"com.google.hydration\",\n \"value\": [\n {\n \"fpVal\": 0.3\n }\n ]\n }\n ]\n }\n\n**Response**\n\nIf your data point was created successfully, you'll get a [`200 OK`](https://httpstatuses.com/200) HTTP\nresponse status code. The response body contains a JSON representation of\nthe data set.\n\n**CURL command** \n\n $ curl --header \"Authorization: Bearer ya29.yourtokenvalue\" --request PATCH \\\n --header \"Content-Type: application/json;encoding=utf-8\" --data @hydration-data.json \\\n https://www.googleapis.com/fitness/v1/users/me/dataSources/raw:com.google.hydration:407408718192:HydrationSource/datasets/1275753581000000000-1275753581000000000\n\n| **Note:** Use `--request PATCH` (not `--request POST`) in your CURL command when adding data."]]