原地址:http://blog.csdn.net/x_iya/article/details/8583015
用到的函数、API等
1、中央气象台API返回的JSON数据(http://m.weather.com.cn/data/101010100.html)
2、外网获取IP(http://ip.dnsexit.com/index.php)
3、Sqlite(1中的城市代号利用sqlite3实现查询)
4、C++中GB2312字符串和UTF-8之间的转换(见这篇文章http://blog.csdn.net/lgh1992314/article/details/8579206)
5、Jsoncpp(主要是处理1中的数据==见这篇文章http://blog.csdn.net/lgh1992314/article/details/8582179)
...
程序依赖于数据库中的城市以及城市代码的丰富度,所以有的城市可能会查询不到哦。
半成品:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
有道的API不能用了,所以换成了新浪的APIhttp://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=58.57.91.184
返回数据依旧是json,但是是utf8编码,所以要做个转换。
jsoncpp修改版支持了utf8 http://blog.csdn.net/x_iya/article/details/16833663
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <locale>
#include <codecvt>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <sqlite3.h>
#include <afxinet.h>
#include <windows.h>
#include "json/json.h"
#pragma comment(lib,"sqlite3.lib")
#pragma comment(lib,"json_vc71_libmtd.lib")
using namespace Json;
using namespace std;
std::string City_code;
std::string country;
std::string city;
std::string province;
std::string isp;
int sqlite3_exec_callback(void *data, int n_columns, char **col_values, char **col_names)
{
//int i;
/*for(i=0; i < n_columns; i++)
{
cout << col_values[i] << "\t";
}
cout << endl;*/
City_code = col_values[0];
return 0;
}
void ConvertUTF8ToANSI()
{
auto LocUtf8 = std::locale(std::locale(""), new std::codecvt_utf8<wchar_t>);
std::wifstream wfin("temp.json");
std::wstring wstr, content;
wfin.imbue(LocUtf8);
while(getline(wfin, wstr))
{
content += wstr;
}
wfin.close();
system("del temp.json");
//std::wcout.imbue(std::locale(""));
//std::wcout << content << std::endl;
std::locale::global(std::locale("Chinese-simplified"));
std::wofstream wfout("weather.json");
wfout << content;
wfout.close();
}
void jsonDate(string strValue) //Jsoncpp
{
//Json::StyledWriter style_write;
Json::Value value;
Json::Reader reader;
reader.parse(strValue, value);
//std::cout << style_write.write(value) << std::endl;
std::cout << value["weatherinfo"]["date_y"].asString() << " ";
std::cout << value["weatherinfo"]["week"].asString() << std::endl;
std::cout << "================================================" << std::endl;
std::cout << "= " << value["weatherinfo"]["temp1"].asString() << " " << " "
<< value["weatherinfo"]["weather1"].asString() << value["weatherinfo"]["wind1"].asString() << std::endl;
std::cout << "= " << value["weatherinfo"]["temp2"].asString() << " "
<< value["weatherinfo"]["weather2"].asString() << " " << value["weatherinfo"]["wind2"].asString() << std::endl;
std::cout << "= " << value["weatherinfo"]["temp3"].asString() << " "
<< value["weatherinfo"]["weather3"].asString() << " " << value["weatherinfo"]["wind3"].asString() << std::endl;
std::cout << "= " << value["weatherinfo"]["temp4"].asString() << " "
<< value["weatherinfo"]["weather4"].asString() << " " << value["weatherinfo"]["wind4"].asString() << std::endl;
std::cout << "= " << value["weatherinfo"]["temp5"].asString() << " "
<< value["weatherinfo"]["weather5"].asString() << " " << value["weatherinfo"]["wind5"].asString() << std::endl;
std::cout << "= " << value["weatherinfo"]["temp6"].asString() << " "
<< value["weatherinfo"]["weather6"].asString() << " " << value["weatherinfo"]["wind6"].asString() << std::endl;
std::cout << "================================================" << std::endl;
}
std::string GetIp()
{
CString url = "http://ip.dnsexit.com/";
CString content;
CString data;
DWORD dwStatusCode;
CInternetSession session("HttpClient");
CHttpFile* pfile = (CHttpFile *)session.OpenURL(url);
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
while (pfile -> ReadString(data))
{
content += data;
}
}
pfile -> Close();
delete pfile;
session.Close();
return std::string(content);
}
void GetInfo(std::string ip)
{
//CFile file((TEXT("temp1.xml")), CFile::modeCreate|CFile::modeWrite);
std::string url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=";
url.append(ip);
CString content;
CString data;
DWORD dwStatusCode;
CInternetSession session("HttpClient");
CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
while (pfile -> ReadString(data))
{
content += data;
}
}
pfile -> Close();
delete pfile;
session.Close();
//file.Write(content, content.GetLength());
//file.Close();
Json::Value value;
Json::Reader reader;
reader.parse(string(content), value);
country = value["country"].asString();
province = value["province"].asString();
city = value["city"].asString();
isp = value["isp"].asString();
}
void GetWeather(std::string city_code)
{
CFile file((TEXT("temp.json")), CFile::modeCreate|CFile::modeWrite);
std::string url = "http://m.weather.com.cn/data/";
url.append(city_code);
url.append(".html");
CString content;
CString data;
DWORD dwStatusCode;
CInternetSession session("HttpClient");
CHttpFile* pfile = (CHttpFile *)session.OpenURL(url.c_str());
pfile -> QueryInfoStatusCode(dwStatusCode);
if(dwStatusCode == HTTP_STATUS_OK)
{
while (pfile -> ReadString(data))
{
content += data;
}
}
pfile -> Close();
delete pfile;
session.Close();
file.Write(content, content.GetLength());
file.Close();
}
void ShowWeather()
{
fstream fin("weather.json");
string strValue, str;
sqlite3 *db = NULL;
char *err