Howdy!
Trying to make GET request with Qt5 and get the response.
But slots never called and no network activity from application.
connect returns true.
Perhaps QNetworkAccessManager get destroyed after calling get method.
Give me explanation how it's works.
Thanks!
appapi.h
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
{
Q_OBJECT
public:
explicit AppApi
(QObject *parent
= 0);
void makeRequest();
QNetworkAccessManager* manager;
signals:
public slots:
void replyFinished(QNetworkReply* reply);
void slotError(QNetworkReply::NetworkError error);
};
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
class AppApi : public QObject
{
Q_OBJECT
public:
explicit AppApi(QObject *parent = 0);
void makeRequest();
QNetworkAccessManager* manager;
signals:
public slots:
void replyFinished(QNetworkReply* reply);
void slotError(QNetworkReply::NetworkError error);
};
To copy to clipboard, switch view to plain text mode
appapi.cpp
{
this->manager = new QNetworkAccessManager(this);
}
void AppApi::makeRequest()
{
connect(this->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
QNetworkRequest request;
request.
setUrl(QUrl("http://google.com"));
QNetworkReply *reply = this->manager->get(request);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}
void AppApi::replyFinished(QNetworkReply* reply)
{
qDebug("replyFinished");
}
void AppApi::slotError(QNetworkReply::NetworkError error)
{
qDebug("slotError");
}
AppApi::AppApi(QObject *parent) :
QObject(parent)
{
this->manager = new QNetworkAccessManager(this);
}
void AppApi::makeRequest()
{
connect(this->manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
QNetworkRequest request;
request.setUrl(QUrl("http://google.com"));
QNetworkReply *reply = this->manager->get(request);
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(slotError(QNetworkReply::NetworkError)));
}
void AppApi::replyFinished(QNetworkReply* reply)
{
qDebug("replyFinished");
}
void AppApi::slotError(QNetworkReply::NetworkError error)
{
qDebug("slotError");
}
To copy to clipboard, switch view to plain text mode
Bookmarks