09-10-2012 03:38 AM
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.
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.
09-10-2012 04:53 AM
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/doc umentation/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;
}
}
09-10-2012 05:15 AM - edited 09-10-2012 05:23 AM
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.
09-13-2012 02:14 AM
Ok the problem is resolved. Error was in instantiation of MyNetwork object
Thanks All for your help.