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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Highlighted
Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

How to create a simple request to a web server and accepting response from it in Cascades ?

I was trying to create an Http Request to interact with a remote server but that didn't worked out. Idid posted about it but didn't found any response.

 

http://supportforums.blackberry.com/t5/Cascades-Development/Error-while-establishing-a-Http-connecti...

 

 

I need an example to implement a request - response methodology in Cascades. I mean I have a remote server that possess some data (lets take some xml or json) that i want to fetch and display on my Ui.

Always Success
Developer
Posts: 107
Registered: ‎05-11-2011
My Device: BlackBerry Z10 LE
My Carrier: AirTel

Re: How to create a simple request to a web server and accepting response from it in Cascades ?

Hi,

 

i tried your code . it's complete working . it's good practice to use

connect statement before mNetworkMgr->get() .

may be reason of error  access_internet permissioin. so plese set it.

 

if you using simulator then may be possible to vmware network adapter setting problem.

 

HPP

#ifndef MYNETWORK_HPP_
#define MYNETWORK_HPP_

#include<QObject>
#include <QtNetwork/qnetworkaccessmanager.h>
#include<QtNetwork/qnetworkreply.h>
#include<QtNetwork/qnetworkrequest.h>
#include<QtNetwork/qnetworksession.h>
#include <QtNetwork/qnetworkconfiguration.h>
#include <QtNetwork/qnetworkconfigmanager.h>
#include<QtNetwork/qnetworkinterface.h>


/*!
 * @brief Network Connection Implementation
 */
class MyNetwork:public QObject
{
	Q_OBJECT;

	public :
		MyNetwork();

		/*!
		 * @brief Method to create and send request
		 */
		void sendRequest();
		/*!
		 * @brief Method to check if network is available
		 */
		bool isNetworkAvailable();

	private slots:
			void requestFinished(QNetworkReply *reply);

};


#endif /* MYNETWORK_HPP_ */

 CPP

#include<app.hpp>
#include<QtNetwork/qnetworkconfigmanager.h>
#include<QtNetwork/qnetworkconfiguration.h>
#include<QList>

QNetworkAccessManager *mNetworkMgr;

MyNetwork::MyNetwork() {

	if (isNetworkAvailable()) {
		sendRequest();
	}

}

void MyNetwork::sendRequest() {

	mNetworkMgr = new QNetworkAccessManager(this);
	QNetworkReply *reply =
			mNetworkMgr->get(
					QNetworkRequest(
							QUrl(
									"http://developer.blackberry.com/cascades/files/documentation/device_platform/networking/model.xml")));
	if (reply) {
		qDebug() << "Reply from server is " << reply;
	}
	bool resFromServer = connect(mNetworkMgr, SIGNAL(finished(QNetworkReply*)),
			this, SLOT(requestFinished(QNetworkReply*)));
	qDebug() << "Connection is success : ? : " << resFromServer;
	Q_ASSERT(resFromServer);
	Q_UNUSED(resFromServer);
}

void MyNetwork::requestFinished(QNetworkReply *reply) {

	if (!reply->error()) {
		// Let's get ALL the data
		const QByteArray response(reply->readAll());

		qDebug() << "Network Method called : " + QString(response);

		// Cleanup
		reply->deleteLater();
	}
}

bool MyNetwork::isNetworkAvailable() {
	QNetworkConfigurationManager netMgr;
	QList<QNetworkConfiguration> mNetList = netMgr.allConfigurations(
			QNetworkConfiguration::Active);
	if (mNetList.count() > 0) {
		if (netMgr.isOnline()) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

 

 

 

Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

Re: How to create a simple request to a web server and accepting response from it in Cascades ?

[ Edited ]

Thanks Raj for your reply. I thought no body will respond about it but you did. Thanks again.

 

Yes i didn't got any device.

Well the network permission was set earlier too.

I also tried what you said about swapping the position of the connect() statement as well as the request statement, but that too didn't worked out.

 

You also told that "vmware network adapter setting problem".
Where should i check for this ?

 

One more thing :

Was slot function requestFinished(QNetworkReply *reply) at your end while you made a run.

Always Success
Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

Re: How to create a simple request to a web server and accepting response from it in Cascades ?

Ok the problem is resolved. Error was in instantiation of MyNetwork object

 

http://supportforums.blackberry.com/t5/Cascades-Development/Error-while-establishing-a-Http-connecti...

 

Thanks All for your help.

Always Success