0.0
- 相关连接
- 代码部分
- 头文件
- 具体实现
相关连接
心知天气官方天气图标
心知天气官网
代码部分
头文件
#include <QtNetwork>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
具体实现
要注意的是心知天气接收的数据应该是Utf8
//天气请求QString weatherReq = 你的请求连接; //创建天气Socket对象QTcpSocket *weatherSocket= new QTcpSocket(this);//连接服务器weatherSocket->connectToHost("api.seniverse.com",80);//等待连接if(weatherSocket->waitForConnected())bool isWeatherServerConnect = true;//发送请求 weatherSocket->write(weatherReq.toUtf8());//获取数据 解析json内容connect(weatherSocket,&QTcpSocket::readyRead,[=](){QByteArray text = weatherSocket->readAll();//读取所有的数据QJsonObject json = QJsonDocument::fromJson(text).object();//获取json 源码if(json.find("results")!=json.end()){QString temp = json["results"].toArray()[0].toObject()["now"].toObject()["temperature"].toString();//获取温度QString weather = json["results"].toArray()[0].toObject()["now"].toObject()["text"].toString();//获取天气QString code =json["results"].toArray()[0].toObject()["now"].toObject()["code"].toString();//获取现在天气的图标信号}});