Receiving JSON from GET request in google apis
-
wrote on 10 Dec 2018, 21:19 last edited by Au7h 12 Oct 2018, 21:20
Hello,
ive a problem with receiving JSON from request.
Headers:
jsontest.hSources:
main.cpp
jsontest.cppmain.cpp below
#include <QCoreApplication> #include "jsontest.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); JsonTest json; json.DoSomething(); return a.exec(); }
jsontest.h below
#ifndef JSONTEST_H #define JSONTEST_H #include <QObject> #include <QtNetwork> #include <QtCore> class JsonTest : public QObject { Q_OBJECT public: explicit JsonTest(QObject *parent = nullptr); void DoSomething(); signals: public slots: void onResult(QNetworkReply *); }; #endif // JSONTEST_H
jsontest.cpp below
#include "jsontest.h" JsonTest::JsonTest(QObject *parent) : QObject(parent) { } void JsonTest::DoSomething() { QNetworkAccessManager networkManager; connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*))); QUrl url("https://www.googleapis.com/customsearch/v1?key=myapihere&cx=017576662512468239146:omuauf_lfve&q=lectures"); QNetworkRequest request; request.setUrl(url); QNetworkReply* currentReply = networkManager.get(request); // GET qDebug() << currentReply->readAll(); currentReply->deleteLater(); } void JsonTest::onResult(QNetworkReply *reply) { qDebug() << "lol"; }
when i copy and paste url in web browser: https://www.googleapis.com/customsearch/v1?key=myapihere&cx=017576662512468239146:omuauf_lfve&q=lectures i will see results but in app the slot does not even execute
-
Hello,
ive a problem with receiving JSON from request.
Headers:
jsontest.hSources:
main.cpp
jsontest.cppmain.cpp below
#include <QCoreApplication> #include "jsontest.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); JsonTest json; json.DoSomething(); return a.exec(); }
jsontest.h below
#ifndef JSONTEST_H #define JSONTEST_H #include <QObject> #include <QtNetwork> #include <QtCore> class JsonTest : public QObject { Q_OBJECT public: explicit JsonTest(QObject *parent = nullptr); void DoSomething(); signals: public slots: void onResult(QNetworkReply *); }; #endif // JSONTEST_H
jsontest.cpp below
#include "jsontest.h" JsonTest::JsonTest(QObject *parent) : QObject(parent) { } void JsonTest::DoSomething() { QNetworkAccessManager networkManager; connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*))); QUrl url("https://www.googleapis.com/customsearch/v1?key=myapihere&cx=017576662512468239146:omuauf_lfve&q=lectures"); QNetworkRequest request; request.setUrl(url); QNetworkReply* currentReply = networkManager.get(request); // GET qDebug() << currentReply->readAll(); currentReply->deleteLater(); } void JsonTest::onResult(QNetworkReply *reply) { qDebug() << "lol"; }
when i copy and paste url in web browser: https://www.googleapis.com/customsearch/v1?key=myapihere&cx=017576662512468239146:omuauf_lfve&q=lectures i will see results but in app the slot does not even execute
Lifetime Qt Championwrote on 10 Dec 2018, 21:21 last edited by mrjj 12 Oct 2018, 21:22Hi and welcome to the forums .
Its a classic.
void JsonTest::DoSomething()
{
QNetworkAccessManager networkManager; <<< local variable. it will be deleted before getting any reply.put it as a member of JsonTest class so it stays alive.
-
wrote on 10 Dec 2018, 21:25 last edited byThis post is deleted!
-
wrote on 10 Dec 2018, 21:28 last edited by
-
wrote on 10 Dec 2018, 21:30 last edited by
ok i commented line currentReply->deleteLater(); and it works :D
-
Hi,
You are trying to read the reply too early as well as deleting it too early. You should do both in the slot connected to the finished signal.
1/6