c++实现查询天气预报

本文介绍了如何利用C++编程从中央气象台和新浪API获取JSON天气数据,并结合Sqlite数据库处理城市代码,进行天气查询。还涉及GB2312与UTF-8字符串转换以及Jsoncpp库的使用。由于依赖数据库的城市信息,部分城市可能无法查询到天气。
摘要由CSDN通过智能技术生成

原地址: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值