Qt HTTP Get Weather Msg

使用QT的接口实现HTTP请求获取天气信息.


所需头文件
JSON相关头文件

1
2
3
4
5
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonArray>

添加HTTP网络管理器

1
#include <QNetworkAccessManager>

请求地址

1
#include <QNetworkRequest>

debug

1
#include <QDebug>

注意

#include 该头文件需要添加在MianWindow下,因为在MianWindow类的定义中定义了:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public slots:
void readDate(QNetworkReply *);
```     
**源码**   
```c
#include "mainwindow.h"
#include "ui_mainwindow.h"
//JSON相关头文件
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonArray>

//添加HTTP网络管理器
#include <QNetworkAccessManager>

//请求地址
#include <QNetworkRequest>

//debug
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //新建一个请求HTTP管理器
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);

    //关联管理器请求
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(readDate(QNetworkReply *)));
    //发送HTTP请求
    manager->get(QNetworkRequest(QUrl("http://t.weather.sojson.com/api/weather/city/101280101")));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::readDate(QNetworkReply *data)
{
    //读取服务器返回的所有数据
    QString msg = data->readAll();
    ui->textBrowser->setText(msg);

    //每次操作JSON时都先判断该字符串是否为JSON格式的
    QJsonParseError err;
    QJsonDocument json = QJsonDocument::fromJson(msg.toUtf8(), &err);
    if(err.error == QJsonParseError::NoError)
        qDebug() << "DATA IS OK";

    //判断完之后,转换成对象
    QJsonObject obj = json.object();
    //提取信息中指定的城市信息这个对象
    QJsonValue value = obj.take("cityInfo");

    //将提取出来的信息转成一个对象
    QJsonObject CityInfo = value.toObject();
    //分别提取相关信息
    value = CityInfo.take("city");
    qDebug() << value.toString() ;

    value = CityInfo.take("citykey");
    qDebug() << value.toString() ;

    value = CityInfo.take("parent");
    qDebug() << value.toString() ;

    value = CityInfo.take("updateTime");
    qDebug() << value.toString() ;
}

当我们取出的value是一个数组时,就将它转成array来处理

1
2
3
4
5
6
QJsonArray arr = value.toArray();

QJsonValue msg = arr.at(0);
QJsonValue msg = arr.at(1);
QJsonValue msg = arr.at(2);
....

效果展示
效果展示

我的GITHUB

发布了32 篇原创文章 · 获赞 1 · 访问量 237

猜你喜欢

转载自blog.csdn.net/qq_41714908/article/details/105239157
今日推荐