信息源是 openweathermap:
调用方式是:
api.openweathermap.org/data/2.5/forecast?id=524901&APPID=1111111111
其中, id是 city ID, APPID需要自己申请,注册一下账号即可自动创建一个默认的ID。
city ID可以从头下面查询到:
http://openweathermap.org/help/city_list.txt
代码分两部分:一部分是抓取天气信息(使用 libcurl库), 另一部分是解析数据。
抓取天气信息部分代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <curl/curl.h>
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
printf("enters here: size: %d\n", size *nmemb);
char *buffer = (char *)stream;
int offset = strlen(buffer);
memcpy(buffer + offset, ptr, nmemb * size);
buffer[offset + nmemb * size] = 0;
return nmemb * size;
}
int main(void)
{
CURL *curl_handle;
char url[1024];
char result[1024 * 1024];
strncpy(url, "http://api.openweathermap.org/data/2.5/forecast?", sizeof(url) -1 );
char *cityId = "id=YOUR_CITY_ID";
char *appId = "APPID=YOUR_APP_ID";
strcpy(url + strlen(url), cityId);
int len = strlen(url);
url[len] = '&';
url[len + 1] = 0;
strcpy(url + strlen(url), appId);
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, url);
/* no progress meter please */
// curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, result);
/* get it! */
curl_easy_perform(curl_handle);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
ParseData(result);
return 0;
}
CityID和 APP ID请用自己的来替换。
解析部分(使用 libjsoncpp库):
#include "json/json.h"
#include <iostream>
extern "C" void ParseData(char *strWeatherData);
void ParseData(char *strWeatherData)
{
Json::Reader reader;
Json::Value root;
if(!reader.parse(strWeatherData, root))
{
return;
}
if( root["list"].isNull())
{
return;
}
int size = root["list"].size();
int i;
for(i = 0; i < size; i++)
{
Json::Value v = root["list"][i];
if(!v["main"].isNull())
{
Json::Value tmp = v["main"];
if( tmp["temp"].isDouble() )
{
std::cout << "temperature: " << tmp["temp"].asFloat() << std::endl;
}
if( tmp["humidity"].isInt())
{
std::cout << "humidity: " << tmp["humidity"].asInt() << std::endl;
}
}
if(v["weather"].isArray())
{
//Json::Value v = v["weather"];
int j;
for(j = 0; j < v["weather"].size(); j++)
{
Json::Value tmp = v["weather"][j];
if(tmp["description"].isString())
{
std::cout << tmp["description"].asString() << std::endl;
}
}
}
if(v["dt_txt"].isString())
{
std::cout << v["dt_txt"].asString() << std::endl;
}
}
}
编译命令:
gcc test2.c -c -g $(pkg-config --cflags libcurl)
g++ parse_weather_data.cpp $( pkg-config --cflags jsoncpp) -g -c
g++ test2.o parse_weather_data.o -o test2 $(pkg-config --libs libcurl jsoncpp)
运行结果如下:
temperature: 299.25
humidity: 95
light rain
2017-08-12 09:00:00
temperature: 298.44
humidity: 96
light rain
2017-08-12 12:00:00
temperature: 299.4
humidity: 95
light rain
2017-08-12 15:00:00
temperature: 298.687
humidity: 95
overcast clouds
References:
1. http://openweathermap.org/appid
2. https://curl.haxx.se/libcurl/c/libcurl.html3. https://www.codeproject.com/articles/1180283/WebControls/
4. http://jsoncpp.sourceforge.net/class_json_1_1_value.html
5. https://www.survivingwithandroid.com/2013/05/build-weather-app-json-http-android.html