Pages

Thursday, September 29, 2011

QJson and Qt Symbian C++

Hi guys...
 I will show an example class to connect with json  parser.
Before compiling the class, you need to download the QJson library from this link http://qjson.sourceforge.net/get_it/


#include "login.h"
#include <QString>
#include <parser.h>
#include <QtNetwork>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QVariant>
#include <QObject>
#include <QMessageBox>
#include "serializer.h"

QString StringList::changeMsisdnUrl;
changeMsisdnService::changeMsisdnService()
{
}
void changeMsisdnService::changeMSISDN(QString userName,QString password){
        QNetworkAccessManager* netAccManager = new QNetworkAccessManager;
        QNetworkRequest request(QUrl(http://www.your loginserviceurl));
 // the url's out put should be a json type. not an object.
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QVariantMap msisdnDetails;
        msisdnDetails.insert("userName",userName);
        msisdnDetails.insert("password",password);
        QJson::Serializer serializer;
        QByteArray json = serializer.serialize(msisdnDetails);
        QNetworkReply *reply = netAccManager->post(request, json);
        QEventLoop loop;
        QObject::connect(reply,SIGNAL(finished()),&loop,SLOT(quit()));
        loop.exec();
        QByteArray bytes = reply->readAll();
               if (reply->error() == QNetworkReply::NoError)
               {
                   QJson::Parser parser;
                   bool ok;
                   QVariantMap result = parser.parse (bytes, &ok).toMap();
                   if (!ok) {
                     qFatal("An error occured during parsing");
                     exit (1);
                   }
                   status=result["status"].toString();
               }else{
               }
               if (status=="Success") {
                    // Your code
               } else {                  
                    // Your code
               }
}





1 comment:

  1. Lucky Club Casino Site | All new players get 100 FS
    Lucky Club Casino is a new and trusted online gambling site founded in 2000. The casino welcomes new players from all luckyclub over the world. Rating: 4 · ‎Review by LuckyClub.live

    ReplyDelete