Slot doesn't get triggered on HTTP reply
-
Hi,
I'm trying to fetch JSON data from a web page using QNetworkRequest, but the slot never gets triggered despite the URL being valid and working. I've tried with any URL and it does not work.
Heres my code :///header file #ifndef DOWNLOADER_H #define DOWNLOADER_H #include <QObject> #include <QJsonObject> #include <QNetworkReply> #include <mutex> #include <condition_variable> namespace Pico{ namespace Downloader{ class Funcs : public QObject { Q_OBJECT public: explicit Funcs(QObject *parent = 0); void DownloadFiles(std::map<std::string, int> &downloadsMap); void PopulateDownloadsMap(QJsonObject game, std::map<std::string, int> downloadsMap); struct currentDownload{ std::condition_variable finished; int state; std::string details; QString data; std::mutex lock; }; signals: public slots: void onAPIResponse(QNetworkReply*); private: }; } } #endif // DOWNLOADER_H
//function to start the download std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl"; /// Fetch download address from API using mod UID const QUrl url = QUrl(QString::fromStdString(modApiPage)); const QNetworkRequest request(url); QNetworkAccessManager networkManager; connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
void Funcs::onAPIResponse(QNetworkReply* reply) { logging.Write("ON_VAULT_RESPONSE => Received data from the API"); ...further operations... }
Am I doing something wrong ? I"ve tried following multiple tutorials but I can"t get this to work.
-
Hi,
I'm trying to fetch JSON data from a web page using QNetworkRequest, but the slot never gets triggered despite the URL being valid and working. I've tried with any URL and it does not work.
Heres my code :///header file #ifndef DOWNLOADER_H #define DOWNLOADER_H #include <QObject> #include <QJsonObject> #include <QNetworkReply> #include <mutex> #include <condition_variable> namespace Pico{ namespace Downloader{ class Funcs : public QObject { Q_OBJECT public: explicit Funcs(QObject *parent = 0); void DownloadFiles(std::map<std::string, int> &downloadsMap); void PopulateDownloadsMap(QJsonObject game, std::map<std::string, int> downloadsMap); struct currentDownload{ std::condition_variable finished; int state; std::string details; QString data; std::mutex lock; }; signals: public slots: void onAPIResponse(QNetworkReply*); private: }; } } #endif // DOWNLOADER_H
//function to start the download std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl"; /// Fetch download address from API using mod UID const QUrl url = QUrl(QString::fromStdString(modApiPage)); const QNetworkRequest request(url); QNetworkAccessManager networkManager; connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
void Funcs::onAPIResponse(QNetworkReply* reply) { logging.Write("ON_VAULT_RESPONSE => Received data from the API"); ...further operations... }
Am I doing something wrong ? I"ve tried following multiple tutorials but I can"t get this to work.
Hi @rackover,
Where you have:
QNetworkAccessManager networkManager; connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
The signal is disconnected and
networkManager
destroyed immediately after the function ends - ie whenaccessManager
goes out of scope. Possibly this is just because you simplified the code to show us the sample, but you should either be allocatingnetworkManager
outside this function, or allocating it on a heap.eg:
QNetworkAccessManager * networkManager = new QNetworkAccessManager(this); connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
Cheers.
-
Hi,
I'm trying to fetch JSON data from a web page using QNetworkRequest, but the slot never gets triggered despite the URL being valid and working. I've tried with any URL and it does not work.
Heres my code :///header file #ifndef DOWNLOADER_H #define DOWNLOADER_H #include <QObject> #include <QJsonObject> #include <QNetworkReply> #include <mutex> #include <condition_variable> namespace Pico{ namespace Downloader{ class Funcs : public QObject { Q_OBJECT public: explicit Funcs(QObject *parent = 0); void DownloadFiles(std::map<std::string, int> &downloadsMap); void PopulateDownloadsMap(QJsonObject game, std::map<std::string, int> downloadsMap); struct currentDownload{ std::condition_variable finished; int state; std::string details; QString data; std::mutex lock; }; signals: public slots: void onAPIResponse(QNetworkReply*); private: }; } } #endif // DOWNLOADER_H
//function to start the download std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl"; /// Fetch download address from API using mod UID const QUrl url = QUrl(QString::fromStdString(modApiPage)); const QNetworkRequest request(url); QNetworkAccessManager networkManager; connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
void Funcs::onAPIResponse(QNetworkReply* reply) { logging.Write("ON_VAULT_RESPONSE => Received data from the API"); ...further operations... }
Am I doing something wrong ? I"ve tried following multiple tutorials but I can"t get this to work.
@rackover said in Slot doesn't get triggered on HTTP reply:
QNetworkAccessManager networkManager;
you create the QNetworkAccessManager isnatnce on the stack, which means it gets automatically deleted on the end of the block it is created in.
When this is not intentional, this is your issue. Create it on the heap (pointer) and set a parent object on it and it should work.Edit: too slow :)
-
@Paul-Colby said in Slot doesn't get triggered on HTTP reply:
QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;Hi,
Thanks from the help from you both. I've tried correcting the code, but the signal still doesn't get triggered.std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl"; /// Fetch download address from API using mod UID const QUrl url = QUrl(QString::fromStdString(modApiPage)); const QNetworkRequest request(url); QNetworkAccessManager * networkManager = new QNetworkAccessManager(this); connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
-
@Paul-Colby said in Slot doesn't get triggered on HTTP reply:
QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;Hi,
Thanks from the help from you both. I've tried correcting the code, but the signal still doesn't get triggered.std::string modApiPage = "https://api.faforever.com/data/mod?page[size]=1&filter=versions.uid=="+uid+"&include=versions&fields[modVersion]=downloadUrl"; /// Fetch download address from API using mod UID const QUrl url = QUrl(QString::fromStdString(modApiPage)); const QNetworkRequest request(url); QNetworkAccessManager * networkManager = new QNetworkAccessManager(this); connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onAPIResponse(QNetworkReply*))) ;
@rackover said in Slot doesn't get triggered on HTTP reply:
QNetworkAccessManager * networkManager = new QNetworkAccessManager(this);
Is this the one QNetworkAccessManager you're using for request?
Also check whether connect() actually succeeded. -
Yes, I believe this is the networkmanager i'm using. How can I check that ?
Connect doesn't return a bool nor a int, I'm not sure of how I should check it's working or not. At least it's not returning any errors. -
Hi,
You should also connect the error related signal.
-
Then connect the error signal of the QNetworkReply you get back when doing the request.
-
-
Might be a silly question but since I don't see it in any of it your code snippet, are you calling QNetworkAccessManager::get at some point ?