#include <webclient.h>
#include <sys/socket.h>
#include <netdb.h>
#include <cJSON.h>
#include <finsh.h>
#define GET_HEADER_BUFSZ 1024 //头部大小
#define GET_RESP_BUFSZ 1024 //响应缓冲区大小
#define GET_URL_LEN_MAX 256 //网址最大长度
#define GET_URI "http://mobile.weather.com.cn/data/sk/%s.html" //获取天气的 API
#define AREA_ID "101021300" //上海浦东地区 ID
{
root = cJSON_Parse((const char *)data);
if (!root)
{
return;
}
object = cJSON_GetObjectItem(root, "sk_info");
item = cJSON_GetObjectItem(object, "cityName");
item = cJSON_GetObjectItem(object, "temp");
item = cJSON_GetObjectItem(object, "wd");
item = cJSON_GetObjectItem(object, "ws");
item = cJSON_GetObjectItem(object, "sd");
item = cJSON_GetObjectItem(object, "date");
item = cJSON_GetObjectItem(object, "time");
cJSON_Delete(root);
}
void weather(int argc, char **argv)
{
int resp_status;
struct webclient_session *session =
RT_NULL;
int content_length = -1, bytes_read = 0;
int content_pos = 0;
{
goto __exit;
}
rt_snprintf(weather_url, GET_URL_LEN_MAX, GET_URI, AREA_ID);
session = webclient_session_create(GET_HEADER_BUFSZ);
{
goto __exit;
}
if ((resp_status = webclient_get(session, weather_url)) != 200)
{
rt_kprintf(
"webclient GET request failed, response(%d) error.\n", resp_status);
goto __exit;
}
{
rt_kprintf(
"No memory for data receive buffer!\n");
goto __exit;
}
content_length = webclient_content_length_get(session);
if (content_length < 0)
{
do
{
bytes_read = webclient_read(session, buffer, GET_RESP_BUFSZ);
if (bytes_read <= 0)
{
break;
}
}while (1);
}
else
{
do
{
bytes_read = webclient_read(session, buffer,
content_length - content_pos > GET_RESP_BUFSZ ?
GET_RESP_BUFSZ : content_length - content_pos);
if (bytes_read <= 0)
{
break;
}
content_pos += bytes_read;
}while (content_pos < content_length);
}
weather_data_parse(buffer);
__exit:
webclient_close(session);
}