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
MFSI
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
Please use plain text.
Developer
raj_jyani
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;
	}
}

 

 

 

Please use plain text.
Developer
MFSI
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
Please use plain text.
Developer
MFSI
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
Please use plain text.