1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| QT += network
#include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply>
void init() { QNetworkAccessManager* manager = new QNetworkAccessManager(this); connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*))); }
void queryWeather() { char quest_array[256]="http://wthrcdn.etouch.cn/weather_mini?city="; QNetworkRequest quest; sprintf(quest_array,"%s%s",quest_array,cityName.toUtf8().data()); quest.setUrl(QUrl(quest_array)); quest.setHeader(QNetworkRequest::UserAgentHeader,"RT-Thread ART"); manager->get(quest); }
void replyFinished(QNetworkReply *reply) { allinfo = reply->readAll(); qDebug()<<"allinfo:"<<allinfo; QJsonParseError err; QJsonDocument json_recv = QJsonDocument::fromJson(allinfo.toUtf8(),&err); qDebug() <<"Json-Error:"<< err.error; if(!json_recv.isNull()) { QJsonObject object = json_recv.object(); } }
|