Qt通过http发送json信息

scorlw 发布于

Qt通过http发送json信息

Qt

已知请求地址和参数格式如下,将本地的信息发送出去。

1
2
3
4
5
6
7
8
http://abc.efg.net/ijk?lmn=pqr
{
"barcode":"code1",
"weight":"1.1",
"length":"2.2",
"width":"3.3",
"height":"4.4"
}

需要使用QNet,核心代码如下:

1
2
3
4
5
6
QUrl url("http://abc.efg.net/ijk?lmn=pqr");
QByteArray array("{\"barcode\":\"ceshi123\nceshi456\",\"weight\":1.11,\"length\":2.22,\"width\":3.33,\"height\":4.44}");//传递字符串需要引号,数值不需要,否则接收端会出现错误
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
//request.setRawHeader("Authorization","Bearer 49152bfddae0c5b5d492d3d9871f8c11");//服务器要求的数据头部,此处没有使用到
manager->post(request,array);

对于Json格式,Qt有自己的类进行支持:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QJsonObject _exampleObject;
_exampleObject.insert("barcode", "ceshi123ceshi456");
_exampleObject.insert("weight", 11.225);
_exampleObject.insert("length", 45.6699);
_exampleObject.insert("width", 0.1);
_exampleObject.insert("height", 1000.456);
//post请求的数据是Json格式。首先创建QJsonObject对象,并插入数据。
m_httpDocum->setObject(_exampleObject);
m_httpData = m_httpDocum->toJson(QJsonDocument::Compact);

QUrl url("http://abc.efg.net/ijk?lmn=pqr");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
m_network->post(request,m_httpData);

注意:因为QJsonObject内部应该是用Hashmap来存储的,所以输出是按key的ASCII码排序来的,可能与要求的顺序不一致,从而导致接收端报错。

另外,jsonArray中插入值的顺序与文件中的顺序是一致的,本身就是数组,自带下标(索引)。

插入值的代码:

1
2
3
4
5
// 构建 Json 数组 - Version
QJsonArray versionArray;
versionArray.append(4.8);
versionArray.append(5.2);
versionArray.append(5.7);

结果:

1
2
3
4
5
"Version": [
4.8,
5.2,
5.7
]

但是这种方式没有key值,不符合本次要求。

所以还是采用直接QByteArray的方式,为具有拓展性,可以采用QString的arg函数。

1
2
3
4
5
6
7
8
QString barcode = "ceshi123ceshi456";
double weight = 1.11;
double length = 2.22;
double width = 3.33;
double height = 4.44;
QString str = tr("{\"barcode\":\"%1\",\"weight\":%2,\"length\":%3,\"width\":%4,\"height\":%5}").arg(barcode).arg(weight).arg(length).arg(width).arg(height);
QByteArray arr = str.toUtf8();//按照Utf-8编码转换,可以转换中文
//QByteArray arr = str.toLatin1();//按照ASCII编码转换,无法转换中文

完整代码如下:

pro:

1
QT       += network

mainwindow.h

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
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QJsonObject>
#include <QJsonDocument>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

void getHtml(QString url);

public slots:
void replyFinished(QNetworkReply* reply);

private slots:
void on_POSTBtn_1_clicked();

private:
Ui::MainWindow *ui;

QNetworkAccessManager* m_network;
QJsonDocument* m_httpDocum;
QByteArray m_httpData;
};
#endif // MAINWINDOW_H

mainwindow.cpp

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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

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

m_network = new QNetworkAccessManager(this);
connect(m_network,&QNetworkAccessManager::finished,this,&MainWindow::replyFinished);

m_httpDocum = new QJsonDocument();
}

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

void MainWindow::getHtml(QString url)
{
m_network->get(QNetworkRequest(QUrl(url)));
}

void MainWindow::replyFinished(QNetworkReply *reply)
{
qDebug() << QString(reply->readAll());
}

void MainWindow::on_POSTBtn_1_clicked()
{
QUrl url("http://abc.efg.net/ijk?lmn=pqr");

QString barcode = "ceshi123ceshi456";
double weight = 1.11;
double length = 2.22;
double width = 3.33;
double height = 4.44;

QString str = tr("{\"barcode\":\"%1\",\"weight\":%2,\"length\":%3,\"width\":%4,\"height\":%5}").arg(barcode).arg(weight).arg(length).arg(width).arg(height);
QByteArray arr = str.toUtf8();

QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
m_network->post(request,arr);
}

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

QString url = "http://abc.efg.net/ijk?lmn=pqr";
w.getHtml(url);
return a.exec();
}