补交 396/4
authorCaisking <3449750381@qq.com>
Fri, 28 Jun 2024 05:09:13 +0000 (13:09 +0800)
committerCaisking <3449750381@qq.com>
Fri, 28 Jun 2024 05:09:13 +0000 (13:09 +0800)
code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp

index e7e1440..88e3c23 100644 (file)
@@ -1,12 +1,15 @@
 #include "health_service.h"
 #include <iostream>
-#include <fstream>
 #include "wearable_log.h"
 #include "cJSON.h"
 #include <sys/stat.h>
 #include <ctime>
 #include "kv_store.h"
 #include "custom_utils.h"
+#include <fstream>
+#include <sstream>
+#include <string>
+#include <cstdint>
 namespace OHOS
 {
 
@@ -112,24 +115,35 @@ namespace OHOS
             return false;
         }
 
-        std::string jsonDataString((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
+        // 读取文件内容到字符串
+        std::stringstream buffer;
+        buffer << inFile.rdbuf();
+        std::string jsonDataString = buffer.str();
         const char *jsonData = jsonDataString.c_str();
         printf("jsonData:%s\r\n", jsonData);
 
         if (jsonDataString.empty())
         {
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " jsonDataString is error");
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "jsonDataString is empty");
+            inFile.close();
+            return false;
+        }
+
+        cJSON *root = cJSON_Parse(jsonData);
+        if (root == NULL)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to parse JSON data");
             inFile.close();
             return false;
         }
 
         if (type == HEALTH_TYPE_HEART)
         {
-            heart_root = cJSON_Parse(jsonData);
+            heart_root = root;
         }
-        if (type == HEALTH_TYPE_OXYGEN)
+        else if (type == HEALTH_TYPE_OXYGEN)
         {
-            oxygen_root = cJSON_Parse(jsonData);
+            oxygen_root = root;
         }
 
         inFile.close();