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. How to connect via API?
QtWS25 Last Chance

How to connect via API?

Scheduled Pinned Locked Moved Solved General and Desktop
66 Posts 8 Posters 11.5k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 26 May 2019, 07:39 last edited by SGaist
    #46

    Thanks. https is working.
    Trying to use the function TIME_SERIES_DAILY_ADJUSTED.
    https://www.alphavantage.co/documentation/
    If you do so it works:

    request.setUrl(QUrl("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=compact&apikey=XXXXXXXXXXXXX"));
    

    If doing so does not work:

    request.setUrl(QUrl("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=XXXXXXXXXXXXXXX"));
    

    It's me doing something wrong or the server is not working?

    [edit: removed API key SGaist]

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 26 May 2019, 20:29 last edited by
      #47

      Since you don't show how you are processing the QNetworkReply object created for your request one can only guess. What signals did you connect from the reply object ? What exactly do you mean by "does not work" ?

      By the way, I've edited out your api key, it's not a good idea to post such information.

      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
      • M Offline
        M Offline
        Mikeeeeee
        wrote on 28 May 2019, 08:51 last edited by
        #48

        It's my h-file:

        private slots:
            void on_testButton_clicked();
            void testSlotFromQDebug();
            void onReply(QNetworkReply* reply);
        
        private:
            Ui::MainWindow *ui;
            QNetworkAccessManager *apiQuery = new QNetworkAccessManager;
        

        it's cpp-file:```
        #include "mainwindow.h"
        #include "ui_mainwindow.h"

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);

        //connect(apiQuery, &QNetworkAccessManager::finished, this, &MainWindow::testSlotFromQDebug);
        

        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        void MainWindow::on_testButton_clicked()
        {
        QNetworkRequest request;
        //request.setUrl(QUrl("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo"));
        request.setUrl(QUrl("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=111111"));
        //request.setUrl(QUrl("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=compact&apikey=1111"));

        QNetworkReply* reply = apiQuery->get(request);
        connect(reply ,&QNetworkReply::finished,this,std::bind(&MainWindow::onReply,this,reply ));
        

        }

        void MainWindow::testSlotFromQDebug()
        {
        //qDebug()<<"point1";
        }

        void MainWindow::onReply(QNetworkReply* reply){
        QJsonDocument jdoc = QJsonDocument::fromJson(reply->readAll());
        //qDebug() << QSslSocket::supportsSsl();
        //qDebug() << QSslSocket::sslLibraryBuildVersionString();
        //qDebug() << QSslSocket::sslLibraryVersionString();
        qDebug()<<jdoc;
        // do something with the jdoc
        }

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 28 May 2019, 20:26 last edited by
          #49

          Again: what do you mean by "it's not working" when using the full output size ?

          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
          0
          • M Offline
            M Offline
            Mikeeeeee
            wrote on 29 May 2019, 06:15 last edited by Mikeeeeee
            #50

            Nothing comes back. I do not receive json document.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 30 May 2019, 22:04 last edited by
              #51

              What about connecting the errors related signals ? That might give you some clues about what is happening.

              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
              0
              • M Offline
                M Offline
                Mikeeeeee
                wrote on 31 May 2019, 12:17 last edited by
                #52

                But what signals and slots to use to find the error?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mikeeeeee
                  wrote on 31 May 2019, 14:05 last edited by
                  #53
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 31 May 2019, 21:39 last edited by
                    #54

                    Maybe QNetworkReply::error ?

                    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
                    • M Offline
                      M Offline
                      Mikeeeeee
                      wrote on 1 Jun 2019, 08:17 last edited by
                      #55

                      I get "QNetworkReply::NoError"

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 1 Jun 2019, 20:24 last edited by
                        #56

                        Did you also check QNetworkReply::sslErrors ?

                        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
                        • M Offline
                          M Offline
                          Mikeeeeee
                          wrote on 3 Jun 2019, 11:15 last edited by
                          #57

                          But it's a signal. And what should be placed instead of const QList<QSslError> &errors?

                          J 1 Reply Last reply 3 Jun 2019, 11:33
                          0
                          • M Mikeeeeee
                            3 Jun 2019, 11:15

                            But it's a signal. And what should be placed instead of const QList<QSslError> &errors?

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 3 Jun 2019, 11:33 last edited by jsulm 6 Mar 2019, 11:35
                            #58

                            @Mikeeeeee said in How to connect via API?:

                            But it's a signal

                            Yes, and you can connect a slot to a signal...
                            "And what should be placed instead of const QList<QSslError> &errors?" - ?
                            Nothing, you implement a slot with same parameter and connect it to the signal as usual:

                            void MyClass::handleSslErrors(const QList<QSslError> &errors)
                            {
                            ...
                            }
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            3
                            • M Offline
                              M Offline
                              Mikeeeeee
                              wrote on 3 Jun 2019, 18:07 last edited by Mikeeeeee 6 Mar 2019, 18:13
                              #59

                              Did so, the slot does not work. So no mistakes.

                              connect(reply, & QNetworkReply::sslErrors , this,  &MainWindow::testSlotFromQDebug);
                              
                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                Mikeeeeee
                                wrote on 3 Jun 2019, 18:19 last edited by
                                #60

                                The server seems to works too, because when you click on the link https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=full&apikey=demo open the file. Maybe this is due to the fact that the file is large?

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 3 Jun 2019, 20:04 last edited by
                                  #61

                                  I tested your query on macOS and it worked fine. What exact version of Windows are you using ?

                                  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
                                  • M Offline
                                    M Offline
                                    Mikeeeeee
                                    wrote on 4 Jun 2019, 06:11 last edited by
                                    #62

                                    Yes, I use Windows

                                    J 1 Reply Last reply 4 Jun 2019, 07:19
                                    0
                                    • M Mikeeeeee
                                      4 Jun 2019, 06:11

                                      Yes, I use Windows

                                      J Offline
                                      J Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on 4 Jun 2019, 07:19 last edited by jsulm 6 Apr 2019, 07:20
                                      #63

                                      @Mikeeeeee Do you think "Yes, I use Windows" is a useful answer to "What exact version of Windows are you using?"?!
                                      We already know that you're using Windows, the question was which exact version?...
                                      Why can't you give exact answers and ask questions which can actually be understood?

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      1
                                      • M Offline
                                        M Offline
                                        Mikeeeeee
                                        wrote on 4 Jun 2019, 07:29 last edited by Mikeeeeee 6 Apr 2019, 07:30
                                        #64

                                        Sorry, I use Windows 7 pro 64bit.

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          Mikeeeeee
                                          wrote on 4 Jun 2019, 15:53 last edited by
                                          #65

                                          The problem turned out to be a large file size. You should read line by line.

                                          Заголовочный файл
                                          #ifndef MAINWINDOW_H
                                          #define MAINWINDOW_H
                                           
                                          #include <QMainWindow>
                                          #include "QDebug"
                                          #include "QtNetwork/QNetworkAccessManager"
                                          #include "QtNetwork/QNetworkRequest"
                                          #include "QtNetwork/QNetworkReply"
                                          #include <QUrl>
                                          #include <QJsonArray>
                                          #include <QJsonDocument>
                                          #include <QJsonObject>
                                          #include <QJsonValue>
                                          #include <QTextCodec>  // для преобразования кодировки
                                          //#include "e_os.h"
                                           
                                          namespace Ui {
                                          class MainWindow;
                                          }
                                           
                                          class MainWindow : public QMainWindow
                                          {
                                              Q_OBJECT
                                           
                                          public:
                                              explicit MainWindow(QWidget *parent = nullptr);
                                              ~MainWindow();
                                           
                                          private slots:
                                              void on_testButton_clicked();
                                              void onReply(QNetworkReply* reply);
                                           
                                          private:
                                              Ui::MainWindow *ui;
                                              QNetworkAccessManager m_apiQuery;
                                          };
                                           
                                          #endif // MAINWINDOW_H
                                           
                                          Файл реализации
                                          #include "mainwindow.h"
                                          #include "ui_mainwindow.h"
                                           
                                          MainWindow::MainWindow(QWidget *parent) :
                                              QMainWindow(parent),
                                              ui(new Ui::MainWindow)
                                          {
                                              ui->setupUi(this);
                                              connect(&m_apiQuery, &QNetworkAccessManager::finished, this, &MainWindow::onReply);
                                          }
                                           
                                          MainWindow::~MainWindow()
                                          {
                                              delete ui;
                                          }
                                           
                                          void MainWindow::on_testButton_clicked()
                                          {
                                              QNetworkRequest request;
                                              request.setUrl(QUrl("https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&outputsize=compact&apikey=63YUZ8NP5SW1D302"));
                                              m_apiQuery.get(request);
                                          }
                                           
                                           
                                          void MainWindow::onReply(QNetworkReply* reply){
                                              QByteArray replyArray;
                                              while (!reply->atEnd()) {
                                                  replyArray.append(reply->readLine());
                                              }
                                              QJsonDocument doc = QJsonDocument::fromJson(replyArray);
                                              qDebug() << doc;
                                              reply->deleteLater();
                                          }
                                          
                                          1 Reply Last reply
                                          1

                                          55/66

                                          1 Jun 2019, 08:17

                                          • Login

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