tags:

views:

31

answers:

1

Hi,
I am using the QJson for parsing. But I am stuck up with some issues. I have used the following code.

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");  
    }  
}  

The output is

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

Please help me .. Where I have done mistake… Thanks is advance.

+2  A: 

The result of the readAll function is an empty byte array. According to documentation this can mean either that no data are available or that an error occurred.

Patrice Bernassola
But I can see the result in the QMessageBox.
Girija
Try to read reply only once. Get it first in a temp variable and then display it in the Message box and using qdebug
Patrice Bernassola
yes .. Thank you very much Patrice. I can see the parsing result now. Thanks a lot. But What is reason? please tell me. Thanks
Girija
You are reading a reply coming from network, once read QNetworkReply wait for a new reply. You can check flag from QIODevice
Patrice Bernassola