4. Send http request
4.1 copy mynetworkobject.cpp and mynetworkobject.h to your project
(ps: these two documents were found on the Internet, and I have modified them)
4.2 add "mynetworkobject.h" to the header file
// I added it in mainwindow.h (no special explanation below, take this demo as an example)
#include "mynetworkobject.h"
4.3 define a networkObj variable in mainwindow.h
//In public
MyNetworkObject *networkObj;
4.4 add a method of request success and failure in mainwindow.h.
public slots:
void requestFail(QString str); //This method is triggered when a "failure signal" is sent
void requestSuccess(QString str);//This method is triggered when a "success signal" is sent
};
4.5 and implement the two slot functions in mainwindow.cpp
void MainWindow::requestFail(QString str)
{
qDebug() << "----------requestFail-------------";
qDebug() << str;
}
void MainWindow::requestSuccess(QString str)
{
qDebug() << "----------requestSuccess-------------";
qDebug() << str;
}
4.6 add in the constructor of mainwindow.cpp
networkObj = new MyNetworkObject(); //Create network objects
connect(networkObj,SIGNAL(requestSuccessSignal(QString)),this,SLOT(requestSuccess(QString))); //Request success signal connection request success method
connect(networkObj,SIGNAL(requestFailSignal(QString)),this,SLOT(requestFail(QString))); //fail
QUrl url("http://1.aosik.applinzi.com/demo/api/getUuid.php"); //Request address
networkObj->get(url); //Send get request
Note: these two signals are defined in the mynetworkobject.h header file. The name can be changed, but remember that other places that use this signal should also be changed together
// Add two signals
signals:
void requestFailSignal(QString str); //Send this signal when the request fails
void requestSuccessSignal(QString str); //Send this signal when the request is successful
4.7 add QT + = network to pro file of the project
QT += network
4.8 add mynetworkobject.cpp to SOURCES and mynetworkobject. H. to HEADERS in the pro file of the project. (PS: remember to add a backslash after the previous sentence. Remember to follow its writing rules. )
SOURCES += main.cpp\
mainwindow.cpp\
mynetworkobject.cpp
HEADERS += mainwindow.h\
mynetworkobject.h
FORMS += mainwindow.ui
Test and rebuild to see if the two files are related in the project. No means failure. Check it carefully.
4.9 if you don't change the signal name I wrote in this step, you don't need to change it.
if(reply->error() == QNetworkReply::NoError) {
qDebug()<<"NoError";
QByteArray buffer = reply->readAll();
dataStr = QString::fromUtf8(buffer);
emit requestSuccessSignal(dataStr); //Trigger success signal here
} else {
emit requestFailSignal(reply->errorString()); //Trigger failure signal here
}
reply->deleteLater();
Let's test the get request together
Here is the log information, perfect!
Debug start
get QUrl( "http://1.aosik.applinzi.com/demo/api/getUuid.php" )
"HttpStatusCodeAttribute = 200"
"HttpReasonPhraseAttribute = OK"
NoError
----------requestSuccess-------------
"{"uuid":"123456789"}"
I unplug the cable and test it
Debug start
get QUrl( "http://1.aosik.applinzi.com/demo/api/getUuid.php" )
"HttpStatusCodeAttribute = "
"HttpReasonPhraseAttribute = "
----------requestFail-------------
"Host 1.aosik.applinzi.com not found"
HOHO, yes~~~
Attach the php code for that test interface
<?php
/**
* User: WiFi
* Date: 2018/03/05
* Time: 09:23
*/
$data = array(
'uuid' => '123456789'
);
//$data = 123456789;
$data = json_encode($data);
echo $data;
demo address: https://github.com/WiFiUncle/blog/tree/master/qtDemo