Qt - 如何使用 QNetworkAccessmanager 获取响应文本
这是我的代码:
Widget::Widget()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
}
void Widget::replyFinished(QNetworkReply* reply)
{
//some other code here
}
我希望 reply 会有一些像 getrespnsetext() 这样的方法,但它不是......
有人能给我举个例子吗,我需要的只是打印出响应文本(有没有像 Javascript Ajax 那样的方式)
感谢您的帮助!
Here is my code:
Widget::Widget()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://qt.nokia.com")));
}
void Widget::replyFinished(QNetworkReply* reply)
{
//some other code here
}
I hope that reply will have some method like getrespnsetext() but it not...
Can some one show me an example, all the thing i need is print out the response text (is ther any way like in Javascript Ajax)
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需在
replyFinished(...)
函数中使用reply->readAll()
即可读取所有返回的文本。它返回一个QByteArray
,因此您可以从那里做任何您想做的事情。You only need to use
reply->readAll()
inside thereplyFinished(...)
function to read all the returned text. It returns aQByteArray
, so you can do wathever you want from there.此处查看
QNetworkReply
的文档 ,特别是在完成信号处,它提到您可以使用 readAll() 来获取所有数据的 QByteArray 。假设您知道此类转换是否有效,QString
确实有一个采用QByteArray
作为参数的构造函数,如文档 此处。Looking at the documentation for
QNetworkReply
here, specifically at the finished signal, it mentions that you can usereadAll()
to get aQByteArray
of all of the data. Assuming that you know whether or not such a conversion is valid,QString
does have a constructor that takes aQByteArray
as a parameter, as documented here.