|
|
#include "weather.h"
|
|
|
#include <QNetworkAccessManager>
|
|
|
#include <QNetworkRequest>
|
|
|
#include <QNetworkReply>
|
|
|
#include <QHttpPart>
|
|
|
#include <QHttpMultiPart>
|
|
|
#include <QJsonDocument>
|
|
|
#include <QJsonObject>
|
|
|
#include <QJsonArray>
|
|
|
#include <QDebug>
|
|
|
#include <QString>
|
|
|
|
|
|
static const QMap<QString, QString> weatherMap {
|
|
|
{"NA", "55"},
|
|
|
{"晴", "0"},
|
|
|
{"多云", "1"},
|
|
|
{"阴", "2"},
|
|
|
{"阵雨", "3"},
|
|
|
{"雷阵雨", "4"},
|
|
|
{"小雨", "7"},
|
|
|
{"中雨", "8"},
|
|
|
{"大雨", "9"},
|
|
|
{"中到大雨", "9"},
|
|
|
{"雪", "13"},
|
|
|
};
|
|
|
static const QMap<QString, QString> weatherMap_night {
|
|
|
{"NA", "55"},
|
|
|
{"晴", "33"},
|
|
|
{"多云", "41"},
|
|
|
{"阴", "35"},
|
|
|
{"阵雨", "47"},
|
|
|
{"雷阵雨", "49"},
|
|
|
{"小雨", "7"},
|
|
|
{"中雨", "8"},
|
|
|
{"大雨", "9"},
|
|
|
{"中到大雨", "9"},
|
|
|
{"雪", "13"},
|
|
|
};
|
|
|
weather::weather(Ui::climate * p)
|
|
|
: exUI(p)
|
|
|
{
|
|
|
manager = new QNetworkAccessManager(this); //新建QNetworkAccessManager对象
|
|
|
/*manager具有异步API,当http请求完成后,会通过finished信号进行通知,
|
|
|
* 接收和发送网络请求都会返回QNetworkReply*/
|
|
|
connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
|
|
|
|
|
|
}
|
|
|
weather::~weather()
|
|
|
{
|
|
|
delete exUI;
|
|
|
}
|
|
|
void weather::getWeather()
|
|
|
{
|
|
|
delete manager;
|
|
|
}
|
|
|
void weather::http_sent()
|
|
|
{
|
|
|
QUrl url;
|
|
|
QString local_city = "101280301";
|
|
|
QNetworkRequest quest;
|
|
|
char quest_array[256]="http://t.weather.sojson.com/api/weather/city/";
|
|
|
sprintf(quest_array,"%s%s",quest_array,local_city.toUtf8().data());
|
|
|
quest.setUrl(QUrl(quest_array));
|
|
|
/*发送get网络请求*/
|
|
|
manager->get(quest);
|
|
|
}
|
|
|
void weather::replyFinished(QNetworkReply *reply)
|
|
|
{
|
|
|
qDebug()<<"The message has been received";
|
|
|
QString all = reply->readAll();//接收的的全部信息
|
|
|
QJsonParseError err;
|
|
|
QJsonDocument json_recv = QJsonDocument::fromJson(all.toUtf8(),&err);
|
|
|
if(!json_recv.isNull())
|
|
|
{
|
|
|
QJsonObject object = json_recv.object();
|
|
|
/*获取更新时间*/
|
|
|
QString SUT = object.value("cityInfo").toObject().value("updateTime").toString();
|
|
|
qDebug()<<"updata:\n"<< SUT;
|
|
|
/*获取温度*/
|
|
|
QJsonObject JO_data = json_recv.object().value("data").toObject();
|
|
|
QString wendu = JO_data.value("wendu").toString() + "°C";
|
|
|
|
|
|
// qDebug()<<"temp:\n"<< wendu;
|
|
|
/*获取湿度*/
|
|
|
QString shidu = JO_data.value("shidu").toString();
|
|
|
/*获取空气质量*/
|
|
|
QString quality = JO_data.value("quality").toString();
|
|
|
//qDebug()<<"quality:\n"<< quality;
|
|
|
/*获取天气预报*/
|
|
|
QJsonArray JA_forecast = JO_data.value("forecast").toArray();
|
|
|
for (int i=0; i<5; i++)
|
|
|
{
|
|
|
/*获取星期*/
|
|
|
QString week =JA_forecast[i].toObject().value("week").toString();
|
|
|
/*获取最大温度*/
|
|
|
QString high_temp =JA_forecast[i].toObject().value("high").toString();
|
|
|
/*获取最大温度*/
|
|
|
QString low_temp =JA_forecast[i].toObject().value("low").toString();
|
|
|
/*获取天气状态,多云*/
|
|
|
QString wtype = JA_forecast[i].toObject().value("type").toString();
|
|
|
/*获取风向*/
|
|
|
QString wind =JA_forecast[i].toObject().value("fx").toString();
|
|
|
/*获取注意事项*/
|
|
|
QString notice =JA_forecast[i].toObject().value("notice").toString();
|
|
|
QStringList list = SUT.split(":");//QString字符串分割函数
|
|
|
/*构造图标的路径*/
|
|
|
QString icon_path = ":/weather/image/" + weatherMap_night[wtype] + ".png";
|
|
|
/*晚上7点到早上六点换不同的图片*/
|
|
|
if((list[0].toInt()>19)||(list[0].toInt()>6))
|
|
|
{
|
|
|
QString icon_path = ":/weather/image/" + weatherMap[wtype] + ".png";
|
|
|
}
|
|
|
|
|
|
if(i==0)
|
|
|
{
|
|
|
exUI->temp_now->setText(wendu);
|
|
|
exUI->label_3->setText(wtype + " "+ high_temp + "/" +low_temp);
|
|
|
exUI->label_5->setText(wtype);
|
|
|
exUI->label_7->setText(high_temp + "/" +low_temp);
|
|
|
exUI->label_8->setText(wind);
|
|
|
QPixmap pixmap(icon_path);
|
|
|
exUI->label->setPixmap(pixmap.scaled(180,180));
|
|
|
exUI->label_6->setPixmap(pixmap.scaled(50,50));
|
|
|
exUI->notice->setAlignment(Qt::AlignCenter);
|
|
|
exUI->notice->setText(notice);
|
|
|
}
|
|
|
if(i==1)
|
|
|
{
|
|
|
exUI->label_30->setText(week);
|
|
|
exUI->label_29->setText(wtype);
|
|
|
exUI->label_32->setText(high_temp + "/" +low_temp);
|
|
|
exUI->label_33->setText(wind);
|
|
|
QPixmap pixmap(icon_path);
|
|
|
exUI->label_31->setPixmap(pixmap.scaled(50,50));
|
|
|
}
|
|
|
if(i==2)
|
|
|
{
|
|
|
exUI->label_35->setText(week);
|
|
|
exUI->label_34->setText(wtype);
|
|
|
exUI->label_37->setText(high_temp + "/" +low_temp);
|
|
|
exUI->label_38->setText(wind);
|
|
|
QPixmap pixmap(icon_path);
|
|
|
exUI->label_36->setPixmap(pixmap.scaled(50,50));
|
|
|
}
|
|
|
if(i==3)
|
|
|
{
|
|
|
exUI->label_40->setText(week);
|
|
|
exUI->label_39->setText(wtype);
|
|
|
exUI->label_42->setText(high_temp + "/" +low_temp);
|
|
|
exUI->label_43->setText(wind);
|
|
|
QPixmap pixmap(icon_path);
|
|
|
exUI->label_41->setPixmap(pixmap.scaled(50,50));
|
|
|
}
|
|
|
if(i==4)
|
|
|
{
|
|
|
exUI->label_45->setText(week);
|
|
|
exUI->label_44->setText(wtype);
|
|
|
exUI->label_47->setText(high_temp + "/" +low_temp);
|
|
|
exUI->label_48->setText(wind);
|
|
|
QPixmap pixmap(icon_path);
|
|
|
exUI->label_46->setPixmap(pixmap.scaled(50,50));
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
qDebug()<<"json_recv is NULL or is not a object !!";
|
|
|
}
|
|
|
reply->deleteLater();
|
|
|
}
|