Why my app don't work?
-
Hi
Why my app don't work?
I want to get data from download link and write to directory{ QNetworkAccessManager netaccessmanager1; QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText()))); QFile file (urldirectory.toString() ); file.open(QIODevice::WriteOnly); QDataStream out(&file); out << reply; }
-
I am suggesting to start out with one of the examples in detailed description of QNetworkManager. Also this example may be of help for you.
For the first two statement, it takes usually a moment for teh reply, but you are not waiting neither are you using a connect statement.
-
@koahnig Thanks
So i send request by this code : QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText())));My code is wrong?
-
the documentation says :
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object opened for reading which emits the readyRead() signal whenever new data arrives.The contents as well as associated headers will be downloaded.
However, in your implementation the app is probably already exiting before the reply has been received. This means you are already past your out statement.
Assuming that the rest of the code is not completely wrong, the "out << reply;" will output only the address where the reply is located, but not the content. You are creating a memory leak there too.Please check out the examples already given above. When you download this example you get a working example and start to modify to your needs. This shall ensure that get the proper understanding of what you are doing and how to do it next time.
-
Not ideal (as if an error occurs it will probably hang forever) but probably this works:
QNetworkAccessManager netaccessmanager1; QFile file (urldirectory.toString() ); file.open(QIODevice::WriteOnly); QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl(ui->textEdit->toPlainText()))); QEventLoop blocker; QObject::connect(reply,&QNetworkReply::readyRead,[&file,&reply]()->void{file.write(reply->readAll());}); QObject::connect(reply,&QNetworkReply::finished,[&file]()->void{file.close();}); QObject::connect(reply,&QNetworkReply::finished,&blocker,&QEventLoop::quit); QObject::connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater); blocker.exec();
-
@VRonin Thanks but this get errors :|
-
yep, sorry, file is not a pointer and you should use
write
instead ofappend
. Updated the code above now.This example works:
QNetworkAccessManager netaccessmanager1; QFile file ("C:/Temp/Nyrss.xml" ); file.open(QIODevice::WriteOnly); QNetworkReply *reply = netaccessmanager1.get(QNetworkRequest(QUrl("http://rss.nytimes.com/services/xml/rss/nyt/World.xml"))); QEventLoop blocker; QObject::connect(reply,&QNetworkReply::readyRead,[&file,&reply]()->void{file.write(reply->readAll());}); QObject::connect(reply,&QNetworkReply::finished,[&file]()->void{file.close();}); QObject::connect(reply,&QNetworkReply::finished,&blocker,&QEventLoop::quit); QObject::connect(reply,&QNetworkReply::finished,reply,&QNetworkReply::deleteLater); blocker.exec();