Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 164
Registered: ‎07-23-2013
My Device: Blackberry z10
My Carrier: Airtel
Accepted Solution

webservice help - json

1. I need to load data

 

import bb.cascades 1.0
import bb.data 1.0
Page {
content: ListView {
id: listView
dataModel: dataModel
//...
}
attachedObjects: [
GroupDataModel {
id: dataModel
},
DataSource {
id: dataSource
source: "http://192.168.1.251:410/Mobile/Service1.svc/english/Category?CountryID=1"
onDataLoaded: {
dataModel.insertList(data)
}
}
]
onCreationCompleted: {
dataSource.load();
}
}

___________________________________________________________________________________________

*************Plese *********LIKE**** & ****ACCEPT as Solution*** If you got suggestion for your Query.

@RAJESH SVM ----- Blackberry Mobile App Developer, Blaze, India.
Developer
Posts: 164
Registered: ‎07-23-2013
My Device: Blackberry z10
My Carrier: Airtel

Re: webservice help - json

i want to view catrrgories from that link.. please anybody help for me..

 

___________________________________________________________________________________________

*************Plese *********LIKE**** & ****ACCEPT as Solution*** If you got suggestion for your Query.

@RAJESH SVM ----- Blackberry Mobile App Developer, Blaze, India.
Highlighted
Developer
Posts: 164
Registered: ‎07-23-2013
My Device: Blackberry z10
My Carrier: Airtel

Re: webservice help - json

1.QML FILE

 

import bb.cascades 1.0
import bb.data 1.0

Page {
content: Container {
layout: StackLayout {
orientation: LayoutOrientation.TopToBottom

}

TextField {
id: countryID
hintText: "Enter Country ID eg:'1'"
maxWidth: 400
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
textStyle.textAlign: TextAlign.Center

}


Button {
id: btn

text: "Send JSON Request"
onClicked: {
app.sendRequest(countryID.text);

}
verticalAlignment: VerticalAlignment.Center
horizontalAlignment: HorizontalAlignment.Center
}

}
}

 

 

 

2.HPP FILE

 

// Default empty project template
#ifndef CALCI_HPP_
#define CALCI_HPP_

#include <QObject>


namespace bb { namespace cascades { class Application; }}

 

class controller : public QObject
{
Q_OBJECT
public:
controller(bb::cascades::Application *app);


public Q_SLOTS:
void sendRequest(const QString &countryID);


private Q_SLOTS:

void onFinished();

};


#endif

 

 

3.CPP FILE

 

// Default empty project template
#include "calci.hpp"

#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>
#include <bb/data/JsonDataAccess>

using namespace bb::cascades;
using namespace bb::data;

controller::controller(bb::cascades::Application *app)
: QObject(app)
{

QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("app", this);

AbstractPane *root = qml->createRootObject<AbstractPane>();

app->setScene(root);
}


void controller::sendRequest(const QString &countryID)
{

QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);

const QString queryUri = QString::fromLatin1("http://192.168.1.251:410/Mobile/Service1.svc/english/Category?CountryID=%1").arg(countryID);

QNetworkRequest request(queryUri);

QNetworkReply* reply = networkAccessManager->get(request);

bool ok = connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
Q_ASSERT(ok);
Q_UNUSED(ok);
}


void controller::onFinished()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
QString response;
if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200)
{
JsonDataAccess jda;
QVariantMap map = jda.loadFromBuffer(reply->readAll()).toMap();

QVariantList addresses = map["GetCategoryResult"].toList();

foreach(QVariant var, addresses) {
QVariantMap addressMap = var.toMap();

qDebug() << "CategoryName is " << addressMap["CategoryName"].toString();
qDebug() << "CategoryID is " << addressMap["CategoryID"].toString();
}
}
else {
qDebug() << "Server returned code " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
}

 

 

Finally i got result for json web service

 

 

___________________________________________________________________________________________

*************Plese *********LIKE**** & ****ACCEPT as Solution*** If you got suggestion for your Query.

@RAJESH SVM ----- Blackberry Mobile App Developer, Blaze, India.