通过调用天气服务器接口来获取天气信息,随后对获取到的json格式信息进行提取和转换
这里给出一个获取天气服务器的接口:http://wthrcdn.etouch.cn/weather_mini?city=
运行结果如下:
获取信息功能实现:
weather.cpp:
#include "weather.h"
Weather
::Weather(QObject
*parent
) : QObject(parent
)
{
manager
= new QNetworkAccessManager(this);
connect(manager
,SIGNAL(finished(QNetworkReply
*)),this,SLOT(replyFinished(QNetworkReply
*)));
}
Weather
::Weather(QString cityName
)
{
manager
= new QNetworkAccessManager(this);
connect(manager
,SIGNAL(finished(QNetworkReply
*)),this,SLOT(replyFinished(QNetworkReply
*)));
this->CityName
=cityName
;
refresh();
}
void Weather
::refresh()
{
this->TemperMax
.clear();
this->currentTemper
.clear();
this->Wind
.clear();
this->WeatherType
.clear();
this->Fever
.clear();
this->allInfo
.clear();
queryWeather();
dataMap
.clear();
}
void Weather
::queryWeather()
{
char request
[256]="http://wthrcdn.etouch.cn/weather_mini?city=";
QNetworkRequest quest
;
sprintf(request
,"%s%s",request
,CityName
.toUtf8().data());
quest
.setUrl(QUrl(request
));
quest
.setHeader(QNetworkRequest
::UserAgentHeader
,"RT-Thread ART");
manager
->get(quest
);
}
QMap
<QString
, QMap
<QString
, QString
> > Weather
::getDataMap(bool *ok
)
{
bool Oktemp
;
if(ok
==nullptr)
{
ok
=&Oktemp
;
}
if(!this->dataMap
.isEmpty())
{
*ok
=true;
return this->dataMap
;
}
*ok
=false;
if(!this->isGetData
)
return this->dataMap
;
QJsonParseError err
;
QJsonDocument json_recv
=QJsonDocument
::fromJson(allInfo
.toUtf8(),&err
);
if(!json_recv
.isNull())
{
QJsonObject object
=json_recv
.object();
if(object
.contains("data"))
{
QJsonValue value
=object
.value("data");
if(value
.isObject())
{
QJsonObject object_data
=value
.toObject();
if(object_data
.contains("yesterday")&&object_data
.contains("forecast"))
{
QJsonValue value
=object_data
.value("yesterday");
if(value
.isObject())
{
QMap
<QString
,QString
>mapvalue
;
mapvalue
["high"]=value
.toObject().value("high").toString();
mapvalue
["low"]=value
.toObject().value("low").toString();
mapvalue
["fengxiang"]=value
.toObject().value("fx").toString();
mapvalue
["fengli"]=value
.toObject().value("fl").toString();
mapvalue
["type"]=value
.toObject().value("type").toString();
dataMap
[value
.toObject().value("date").toString()]=mapvalue
;
}
value
=object_data
.value("forecast");
if(value
.isArray())
{
QJsonArray valueArray
=value
.toArray();
for(int i
=0;i
<valueArray
.count();i
++)
{
QJsonObject object
=valueArray
.at(i
).toObject();
QMap
<QString
,QString
>mapvalue
;
mapvalue
["high"]=object
.value("high").toString();
mapvalue
["low"]=object
.value("low").toString();
mapvalue
["fengxiang"]=object
.value("fengxiang").toString();
mapvalue
["fengli"]=object
.value("fengli").toString();
mapvalue
["type"]=object
.value("type").toString();
dataMap
[object
.value("date").toString()]=mapvalue
;
}
*ok
=true;
return dataMap
;
}
}
}
}
}
return dataMap
;
}
void Weather
::replyFinished(QNetworkReply
*reply
)
{
this->isGetData
=false;
allInfo
=reply
->readAll();
QJsonParseError err
;
QJsonDocument json_recv
=QJsonDocument
::fromJson(allInfo
.toUtf8(),&err
);
if(!json_recv
.isNull())
{
QJsonObject object
=json_recv
.object();
if(object
.contains("data"))
{
QJsonValue value
=object
.value("data");
if(value
.isObject())
{
QJsonObject object_data
=value
.toObject();
this->CityName
=object_data
.value("city").toString();
this->currentTemper
=object_data
.value("wendu").toString();
this->Fever
=object_data
.value("ganmao").toString();
if(object_data
.contains("forecast"))
{
QJsonValue value
=object_data
.value("forecast");
if(value
.isArray())
{
QJsonObject today_weather
=value
.toArray().at(0).toObject();
WeatherType
=today_weather
.value("type").toString();
currentDate
=today_weather
.value("date").toString();
QString low
=today_weather
.value("low").toString();
QString high
=today_weather
.value("high").toString();
TemperMax
=low
.mid(low
.length()-3,4)+"-"+high
.mid(high
.length()-3,4);
QString windStrength
=today_weather
.value("fengli").toString();
windStrength
.remove(0,8);
windStrength
.remove(windStrength
.length()-2,2);
Wind
=today_weather
.value("fengxiang").toString()+windStrength
;
this->isGetData
=true;
}
}
}
}
}
reply
->deleteLater();
if(isGetData
)
{
emit
this->getDataSuccessedSignal();
}
else
{
emit
this->getDataFailedSignal();
}
emit
this->getDataFinisedSignal();
}
QString Weather
::getAllInfo()
{
return this->allInfo
;
}
void Weather
::setCityName(QString city
)
{
this->CityName
=city
;
}
QString Weather
::GetCityName()
{
return this->CityName
;
}
QString Weather
::GetDate()
{
return this->currentDate
;
}
QString Weather
::GetTemperature()
{
return this->currentTemper
;
}
QString Weather
::GetTemperatureMax()
{
return this->TemperMax
;
}
QString Weather
::GetWind()
{
return this->Wind
;
}
QString Weather
::GetFluenza()
{
return this->Fever
;
}
QString Weather
::GetWeatherType()
{
return this->WeatherType
;
}
weather.h:
#ifndef WEATHER_H
#define WEATHER_H
#include<QObject>
#include<QNetworkAccessManager>
#include<QNetworkReply>
#include<QNetworkRequest>
#include<QJsonDocument>
#include<QJsonArray>
#include<QJsonObject>
class Weather : public QObject
{
Q_OBJECT
public:
explicit Weather(QObject
*parent
= nullptr);
explicit Weather(QString cityName
= "");
public:
void refresh();
void setCityName(QString city
);
QString
GetCityName();
QString
GetDate();
QString
GetTemperature();
QString
GetTemperatureMax();
QString
GetWind();
QString
GetWeatherType();
QString
GetFluenza();
QString
getAllInfo();
void queryWeather();
QMap
<QString
, QMap
<QString
, QString
> > getDataMap(bool *ok
=nullptr);
private:
QNetworkAccessManager
*manager
;
QString allInfo
;
QString CityName
;
QString currentDate
;
QString currentTemper
;
QString TemperMax
;
QString Wind
;
QString WeatherType
;
QString Fever
;
bool isGetData
=false;
QMap
<QString
,QMap
<QString
,QString
>> dataMap
;
public slots
:
void replyFinished(QNetworkReply
*reply
);
signals
:
void getDataFinisedSignal();
void getDataSuccessedSignal();
void getDataFailedSignal();
};
#endif
源码:
https://download.csdn.net/download/NuvoleWalker/12806563
转载请注明原文地址:https://tech.qufami.com/read-18058.html