Read Data From URL
-
Hello Friends And Qt Experts
i am trying to read data from any particular URL
But between the process my application is hang or not responding
qDebug() << "Pointer --> 1"; QNetworkAccessManager manager; qDebug() << "Pointer --> 2"; QNetworkReply *response = manager.get(QNetworkRequest(QUrl(QString::fromStdString("MY_URL")))); qDebug() << "Pointer --> 3"; QEventLoop event; qDebug() << "Pointer --> 4"; QObject::connect(response, SIGNAL(finished()), &event, SLOT(quit())); qDebug() << "Pointer --> 5"; event.exec(); qDebug() << "Pointer --> 6"; QString content = response->readAll(); qDebug() << "Pointer --> 7"; std::string readBuffer = content.toStdString(); qDebug() << "Pointer --> 8";
When my pointer is reach At event.exec() that time my application is hang or not responding
i am not getting Next message after "Pointer --> 5"
please help to solve this problem
Thanks in Advance
-
@Ketan__Patel__0011
Why are you using anyevent.exec()
? You are supposed to read from the response in thefinished()
slot, with no quitting. (Your response/reply/manager object should be scoped so it/they live until then.) -
@Ketan__Patel__0011 said in Read Data From URL:
When my pointer is reach At event.exec() that time my application is hang or not responding
Of course it does. exec() is a blocking call. Why do you use a local event loop?
Qt is an asynchronous framework, you should use it as such.
There are also networking examples showing how to do it properly. -
@Ketan__Patel__0011 said in Read Data From URL:
and run the application then i didn't the response.
Because your QNetworkAccessManager manager; is a local variable...
-
@Ketan__Patel__0011
That's why I wroteYour response/reply/manager object should be scoped so it/they live until then.
So as @jsulm is saying, you have to look at the scope/lifetime of the variables you are using.
-
-
@Ketan__Patel__0011 Do you mean finished signal was not emitted?
You should add error handling to your code.
And you CAN'T read the response just after sending the request, do it in a slot connected to finished signal... -
@Ketan__Patel__0011 As I said: you can't read response just after sending request. This is not how asynchronous APIs work. Read response in a slot connected ti finished signal. This is also what @JonB already wrote.
-
@Ketan__Patel__0011 Did you read the documentation? https://doc.qt.io/qt-5/qnetworkaccessmanager.html
QNetworkAccessManager *manager = new QNetworkAccessManager(this); connect(manager, &QNetworkAccessManager::finished, this, &MyClass::replyFinished); manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
In MyClass::replyFinished you read the response.
And there are many examples: https://doc.qt.io/qt-5/examples-network.html
-
Thanks for the reply
I am try all are the solution
And Create One Small QWidget Application Example For it and my problem is solved but..
i am create one library in Qt any put my same code in my Class library functions
that time i didn't get the response from my URL. -
@Ketan__Patel__0011 In what app do you use that library? Does that app have a running Qt event loop?
-
@Ketan__Patel__0011 But does this application have a Qt event loop?
-
@Ketan__Patel__0011 said in Read Data From URL:
But My Library Is Qt Based
That is clear. But without Qt event loop Qt networking isn't going to work. You could execute networking part (from your DLL) in a thread with Qt event loop.