How to connect via API?
-
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
} -
Again: what do you mean by "it's not working" when using the full output size ?
-
What about connecting the errors related signals ? That might give you some clues about what is happening.
-
Maybe QNetworkReply::error ?
-
Did you also check QNetworkReply::sslErrors ?
-
@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) { ... }
-
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?
-
I tested your query on macOS and it worked fine. What exact version of Windows are you using ?
-
@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? -
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(); }
-
How large is it ?