QNetworkAccessManager和Xml回复

时间:2012-06-13 20:29:06

标签: xml qt qnetworkaccessmanager

我试图使用QNetworkAccessmanager向localhost / conductor / api / provider_accounts.xml发送请求,但问题是当我用浏览器打开它然后我得到了相应的输出但是当我对QNetworkAccessmanager执行相同操作时我就是没有得到任何东西。这是我的代码

  void ServiceProvider::replyFinished(QNetworkReply * reply)
 {
 qDebug()<<reply->readAll();
 QString data=(QString)reply->readAll();
 qDebug()<<data;
 QXmlStreamReader xml(data);
 QXmlStreamAttributes attrib;
    while(!xml.atEnd()){
        xml.readNextStartElement();
        if(xml.name()=="name" && !xml.isEndElement()){
            attrib=xml.attributes();
            QString name=attrib.value("name").toString();
            qDebug()<<name;
            }
    }
}

void ServiceProvider::pushImage()
{
manager = new QNetworkAccessManager(this);
manager->get(QNetworkRequest(QUrl("https://localhost/conductor   
/api/provider_accounts.xml")));
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}

1 个答案:

答案 0 :(得分:0)

您无法阅读两次回复:

qDebug()<<reply->readAll();
QString data=(QString)reply->readAll();

上面的qDebug()行将消耗所有数据。您必须readAll()的第二个电话也不正确,因为它会返回QByteArray。您不能像上面那样简单地将其投射到QString并期望它能够正常工作。您需要检查回复的编码并使用相应的转换函数,例如QString::fromUtf8(reply->readAll())