Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Receiving JSON from GET request in google apis

Receiving JSON from GET request in google apis

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 472 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Au7h
    wrote on last edited by Au7h
    #1

    Hello,

    ive a problem with receiving JSON from request.

    Headers:
    jsontest.h

    Sources:
    main.cpp
    jsontest.cpp

    main.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

    mrjjM 1 Reply Last reply
    1
    • A Au7h

      Hello,

      ive a problem with receiving JSON from request.

      Headers:
      jsontest.h

      Sources:
      main.cpp
      jsontest.cpp

      main.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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi 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.

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Au7h
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • A Offline
          A Offline
          Au7h
          wrote on last edited by
          #4

          0_1544477311971_aaa.png

          still the same

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Au7h
            wrote on last edited by
            #5

            ok i commented line currentReply->deleteLater(); and it works :D

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved