赞
踩
#define TIME_URL "https://api.uukit.com/time" //api接口
我们对其接口发起请求,返回结果如下:
{
"status": 1,
"data": {
"timestamp": 1616556071,
"microtime": 1616556071.259844,
"gmt": "2021-03-24 03:21:11",
"utc": "2021-03-24T03:21:11Z",
"timezone": "Shanghai"
},
"req_id": "3b853233236df4a63608"
}
以下代码实现对该接口的请求,并使用cjson解析出时间信息。
config.url = TIME_URL; //设置url,请求方式
config.method = HTTP_METHOD_GET;
printf("start connect to url = %s\r\n",config.url);
client = esp_http_client_init(&config);
esp_http_client_perform(client); //发起http连接
esp_http_client_close(client);
esp_http_client_cleanup(client);
cJSON *root = cJSON_Parse(http_data); //解析返回的时间json数据
if(root!=NULL)
{
cJSON *time = cJSON_GetObjectItem(root,"data");
time = cJSON_GetObjectItem(time, "gmt");
char *t = cJSON_GetStringValue(time);
if(t==NULL)
{
ESP_LOGI(TAG, "time error");
}else{
ESP_LOGI(TAG, "time = %s",t);
strncpy(str_time, t, 25); //将字符串复制到str_time
cJSON_Delete(root);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。