PDA

View Full Version : issue in downloading using QNetworkAccessManager



ejoshva
26th June 2015, 13:06
I am trying to download from an url using QNetworkAccessManager.



void qmlinterface::onSyncBooks(QString isbn,QString chapNo)
{
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(testDownload(QNetworkReply*)));
QUrl serviceUrl = QUrl("http://172.16.14.41/mod_open1/download_quiz.php");

QByteArray postData;
postData.append("isbn="+isbn);
postData.append("&user_mail="+userName);
postData.append("&section_id="+chapNo);

QNetworkRequest request(serviceUrl);
manager->post(request, postData);
}
void qmlinterface::testDownload(QNetworkReply *pReply)
{
qDebug()<<"error : "<<pReply->errorString();
downloadedData = pReply->readAll();
qDebug()<<"downloadedData size : "<<downloadedData.size();
pReply->deleteLater();
addSyncDetailsToBookPath();

}



Got the response but before the response is completed fully, the control executes the next command

alainstgt
26th June 2015, 14:02
you do not upload the data, so you'll never get the signal finished().

You must add something like:


connect( mpReply, SIGNAL( readyRead() ), SLOT( downloadReadyRead() )); // trigger data download
mpReply = manager.get( request );
...

void qmlinterface::downloadReadyRead()
{
mBuffer += mpReply->readAll);
//qDebug() << "mpBuffer size =" << mpBuffer->size();
}

in class declaration add:

private slots:
void downloadReadyRead();

private:
QNetworkReply* mpReply;
QByteArray mBuffer

Notice that this approach would probably not work correctly if you are sending several requests.
You should use then a queue to store the requests and working down the queue one after the other.

alainstgt
27th June 2015, 01:29
removed... coming tomorow

ChrisW67
27th June 2015, 04:00
Got the response but before the response is completed fully, the control executes the next command
You are not clear what "next command" is.

The network post operation is asynchronous and your application will continue functioning while the network operation is carried out and response is returned. This is expected behaviour. If you need something to happen after the response is received then you should do it/trigger it from the finished() handler (after checking that it finished successfully).