[SOLVED][QHttp] qhttp->get("/index.html?text=AAA",file) Doesn't work correctly
-
Since the docs say QHttp is deprecated, and without testing:
Can't you use the QNetworkAccessManager?
Like so:
@
QUrl myURL(QString("http://website.info/index.html"));
myURL.addQueryItem(QString("text"), QString("some+text"));
QNetworkRequest myRequest(myURL);QNetworkAccessManager myManager(this);
QNetworkReply * pMyReply = myManager.get(myRequest);// now use pMyReply to get the data, which is a subclass of IODevice!
// don't know if the device is already open, if not, use ->open()
// then read
QByteArray byteData = pMyReply->readAll();
// and convert to string!
// be careful, take care of encoding here:
QString stringData(byteData);// and clean up
delete pMyReply;
@ -
I've got a problem in Your code LinusA. I tried to initialize QNetworkRequest myRequest(myURL); I've got "Variable 'QNetworkRequest myReques' has initializer but incompatible type". What could I do wrong? To be sure if everything is as you said I've copied Your code.
-
Possible cause of the problem may be that ::get() call is asynchronous, so you should wait for requestFinished() signal to be emitted before reading. Also, I assume that "text" variable is a QString? And, in first QDebug() you should probably use "file->size()" instead of "plik->size()".
And indeed, QHttp is deprecated, try QNAM, it's fun! :D
-
[quote author="jaszczomb" date="1312299549"]I've got a problem in Your code LinusA. I tried to initialize QNetworkRequest myRequest(myURL); I've got "Variable 'QNetworkRequest myReques' has initializer but incompatible type". What could I do wrong? To be sure if everything is as you said I've copied Your code.[/quote]
Hm, since "Variable 'QNetworkRequest myReques' has initializer but incompatible type" looks like a typo (myReques vs myRequest), could you please post your code and the exact error message with line number, please?As I said, I didn't even compile this code, just gave a rough idea how to use QNetworkAccessManager. It's all in the docs.
-
@ ui->setupUi(this);
connect(ui->przycisk,SIGNAL(clicked()),this,SLOT(pobieranie()));
QUrl url(QString("http://website.info/index.html"));
url.addQueryItem(QString("text"), QString("some+text"));
QNetworkRequest request(url); //12 line in my cpp file
QNetworkAccessManager manager(this);
QNetworkReply * pReply = manager.get(request);
QByteArray byteData = pReply->readAll();
QString stringData(byteData);@
Message: "12: error:variable ‘QNetworkRequest request’ has initializer but incomplete type"
That's all in constructor of an application. Until it's not working I won't write anything more because next step is to find links from the document with .indexOf (that's why I'm converting site to QString).
I'll search the Docs to find an answer but any of your suggestions is very helpful. -
[quote author="jaszczomb" date="1312309394"]
Now i have one more problem with QByteArray byteData = pReply->readAll() but I hope I can make it on my own since now. [/quote]
I could imagine you might have to call
@
if (!pReply->open(QIODevice::ReadOnly)) {
// Error: something went wrong,
}
// you can also check pReply->isOpen() now
pReply->readAll();
@
See docs of QIODevice. -
Be sure and take note of sierdzio's post above. If you call readAll() immediately after the get(), then there more than likely won't be anything to read yet.
You probably want to look at either "QNetworkRequest::finished()":http://doc.qt.nokia.com/4.7/qnetworkreply.html#finished or "QNetworkAccessManager::finished()":http://doc.qt.nokia.com/4.7/qnetworkaccessmanager.html#finished
-
Just a quick additional note on my last post - if you don't want to make your class asynchronous, too, you can wait for the signal right in your constructor, with something like this:
@forever
{
if (/* Check for reply here /)
{
/ Do reading here */
}
else
{
// This ensures that your application will respond to events while waiting for the reply
qApp->processEvents();
}
}
@
Although doing it in the constructor might be dangerous. Also, it will wait for an answer indefinitely... be careful :)