一、获取心知天气函数
把其中的密钥改成自己注册的
char* xinzhi_task(char * city_str)
{
int8_t return_res = 1;
char* weather_buffer = NULL;
int content_length = 0;
char city_temp[] = {"http://api.seniverse.com/v3/weather/now.json?key=SuipJRRw3EKYIeK2Mg&location= &language=zh-Hans&unit=c"};
//char *url_str = utf8_url_encode((const char *)city_str);
//free(city_str);
sprintf( city_temp, "http://api.seniverse.com/v3/weather/now.json?key=SuipJRRw3EKYIeK2M&location=%s&language=zh-Hans&unit=c", "shanghai");
//free(url_str);
ESP_LOGI(TAG, "待打开的url \r\n%s\r\n", city_temp);
esp_http_client_config_t config =
{
.event_handler = _http_event_handler,
.url = city_temp,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
if( client == NULL )
{
return NULL;
}
// GET Request
esp_http_client_set_method(client, HTTP_METHOD_GET);
esp_err_t err = esp_http_client_open(client, 0);
if (return_res && err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
return_res = 0;
}
else
{
content_length = esp_http_client_fetch_headers(client); //获取到之后要读的长度
if (content_length < 0)
{
ESP_LOGE(TAG, "HTTP client fetch headers failed");
return_res = 0;
} else
{
weather_buffer = malloc(content_length); //malloc方式分配存储空间
memset(weather_