QJSON在Mac中使用 - 遇到一些问题

时间:2010-09-17 06:09:38

标签: qt qt4 qjson

我正在使用QJson进行解析。但是我遇到了一些问题。我使用了以下代码:

void CityBook ::getCityList(QUrl url)  
{  
        //!connect(cityReply, SIGNAL(readyRead()),this, SLOT(httpReadyRead()));  
        cityGuideNetworkAccessManager = new QNetworkAccessManager(this);  
        connect(cityGuideNetworkAccessManager, SIGNAL(finished(QNetworkReply*)),  
             this, SLOT(httpReadyRead(QNetworkReply*)));  
     QNetworkRequest cityRequest(url);  
     cityGuideNetworkAccessManager->get(cityRequest);  
}  

void CityBook::httpReadyRead(QNetworkReply *reply)  
{  
    QMessageBox::information(this, tr("HTTP"),  
                              tr(reply->readAll()),QMessageBox::NoButton  
                            );  
    QJson::Parser parser;  
    bool ok;  
    const QByteArray &resultbyte = reply->readAll();  
    qDebug() << resultbyte;  
    QVariant result1 = parser.parse(reply->readAll(), &ok);  
    qDebug() << result1;  
    QVariantList result=parser.parse(resultbyte,&ok).toList();  
    qDebug()<< result.size();  
    if (!ok)  
    {  
        qFatal("An error occurred during parsing");  
        exit (1);  
    }  
    qDebug() <<"error String"<< parser.errorString();   
    qDebug() <<"error" <parser.errorLine();  
    //! QVariantList entries = result["name"].toList();  
    foreach (QVariant city, result) {   
        QVariantMap names = city.toMap();  
        qDebug() << "\t-" << names.value("name");  
    }  
}  

输出结果为:

Starting /Users/QT Developement/CityBook-build-desktop/CityBook.app/Contents/MacOS/CityBook...  
""   
QVariant(, )  
0   
error String ""   
error 0  

1 个答案:

答案 0 :(得分:3)

readAll函数的结果是一个空字节数组。根据文档,这可能意味着没有数据可用或发生错误。