PDA

View Full Version : How to read the contents of the QHTTP file in QString?



Gokulnathvc
16th June 2011, 09:31
How to read the contents of the QHTTP file in QString? I want to read the contents of the file in http and am using QHTTP for it. How to read it in QString or any other variables.

ChrisW67
16th June 2011, 09:57
Read the data into a QByteArray then, assuming the data is actually text, use either:


// QByteArray ba
QString str = QString::fromUtf8(ba.constData(), ba.size());
QString str = QString::fromAscii(ba.constData(), ba.size());
// or one of the other variants

Gokulnathvc
16th June 2011, 10:19
How to read the contents from QHTTP in a bytearray?

FelixB
16th June 2011, 10:26
by using a method that returns a QByteArray. You'll find it in the documentation (http://doc.qt.nokia.com/latest/qhttp.html).

just to mention it the 134281246874th time:

This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

Gokulnathvc
16th June 2011, 11:56
I am able to write the contents into a file, but unable to write those contents in a variable. Whether it is possible of reading the contents directly to a variable rather than writing to a file and then reading??

Added after 1 29 minutes:


QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
QNetworkReply *reply= manager->get(QNetworkRequest(QUrl("http://www.oczenterprise.com/support/firmware/MN0MTV660VYQ0BGL/release/12606")));

QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();

QFile file("C:\\data.dat");

file.open(QIODevice::WriteOnly);

file.write(reply->readAll());
QByteArray strbytes;
strbytes=reply->readAll();
file.close();

QFile ffile("D:\\data.dat");
ffile.open((QIODevice::WriteOnly));
ffile.write(strbytes);

In this code, strbytes has nothing. I have used
QByteArray strbytes;
strbytes=reply->readAll();

ChrisW67
17th June 2011, 01:16
Are you serious?! :confused:

You use the QHttp::readAll() method that returns a QByteArray (you know, a variable in memory) in the code you just posted. It contains nothing because you read the stream when it is already at the end of file, something that has been pointed out to you before. If you cannot work it out from the QHttp documentation, general programming knowledge, your own code, and the numerous other threads on this same problem then I suggest it is time you sat back and reassessed your programmer aspirations.

wysota
17th June 2011, 01:22
A classic facepalm thread :cool:

6579

ChrisW67
17th June 2011, 01:30
I was just about to post this:
6580
because it needs some emphasis... but you and I are obviously tuned into the same meme :D

wysota
17th June 2011, 01:35
I had this one bookmarked for the next post but you beat me to it :D

Edit: Sorry, just can't stop laughing :rolleyes: