步骤
1. 选择 API
我用的是 聚合数据 上的 “天气预报” https://www.juhe.cn/docs/api/id/73
选择它的原因主要是:免费、数据简洁明了
2. 发送网络请求
manager = new QNetworkAccessManager(this);
QObject::connect(manager, &QNetworkAccessManager::finished, this, &WeatherWindow::replyFinished);
//往天气预报 API 发出请求
manager->get(QNetworkRequest(QUrl("http://apis.juhe.cn/simpleWeather/query?city=广州&key=118d24f3f0c7b522a6142d9a0a4f01c9")));
注意:发送网络请求的网络地址开头必须是 http , 不能是 https , 否则无法获取网络响应
- http:超文本传输协议,以明文方式发送内容,不提供任何方式的数据加密
- https:在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密
- 具体关于 http 和 https 的介绍可参考博客 https://www.cnblogs.com/wqhwe/p/5407468.html
3. 接收网络响应获得的json并进行解析
//处理收到的 json 数据
void WeatherWindow::replyFinished(QNetworkReply *reply)
{
QString json_msg = reply->readAll();
qDebug() << json_msg;
//定义一个变量用于记录位置
int position;
weather_tmp = json_msg.split('"');
//设置当天的地点、天气、温度
ui->label_loca_city->setText(weather_tmp[9].toUtf8());
ui->label_local_temperature->setText(weather_tmp[15].toUtf8());
//将保存在本文件中的天气图标用链接显示出来
ui->label_local_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[23])));
//减小 json_msg 的长度, weather_tmp 能存储的内容有限
position = json_msg.indexOf("date");
json_msg = json_msg.remove(0,position);
weather_tmp = json_msg.split('"');
ui->label1_date->setText(weather_tmp[2].toUtf8());
weather_tmp[6].replace(QString("\\/"),QString("—"));
ui->label1_temperature->setText(weather_tmp[6]);
ui->label1_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));
position = json_msg.indexOf("date",4);
json_msg = json_msg.remove(0,position);
weather_tmp = json_msg.split('"');
ui->label2_date->setText(weather_tmp[2].toUtf8());
weather_tmp[6].replace(QString("\\/"),QString("—"));
ui->label2_temperature->setText(weather_tmp[6]);
ui->label2_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));
position = json_msg.indexOf("date",4);
json_msg = json_msg.remove(0,position);
weather_tmp = json_msg.split('"');
ui->label3_date->setText(weather_tmp[2].toUtf8());
weather_tmp[6].replace(QString("\\/"),QString("—"));
ui->label3_temperature->setText(weather_tmp[6]);
ui->label3_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));
position = json_msg.indexOf("date",4);
json_msg = json_msg.remove(0,position);
weather_tmp = json_msg.split('"');
ui->label4_date->setText(weather_tmp[2].toUtf8());
weather_tmp[6].replace(QString("\\/"),QString("—"));
ui->label4_temperature->setText(weather_tmp[6]);
ui->label4_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));
position = json_msg.indexOf("date",4);
json_msg = json_msg.remove(0,position);
weather_tmp = json_msg.split('"');
ui->label5_date->setText(weather_tmp[2].toUtf8());
weather_tmp[6].replace(QString("\\/"),QString("—"));
ui->label5_temperature->setText(weather_tmp[6]);
ui->label5_weather->setStyleSheet(QString("border-image: url(:/pic/%1);").arg(get_weather_pic(weather_tmp[10])));
}
4. 天气图标的显示
//返回天气图标的路径
QString WeatherWindow::get_weather_pic(QString name)
{
if(name == "中雨") return QString("heavy_rain.png");
else if(name == "雷阵雨") return QString("rain_thunder.png");
else if(name == "中雨转雷阵雨") return QString("rain_thunder.png");
else if(name == "多云") return QString("cloudy.png");
else if(name == "晴") return QString("sunny.png");
else return QString("unknown.png");
}