09-07-2012 10:41 AM
I am instantiating MyNetwork class on a button click in my app class.
Hence constructor in MyNetwork class is called in which i am checking network availability.
Once a network is found available, a sendRequest() method is called in which a get request is initiated.
And the QNetworkAccessManager object emits finished(QNetworkReply *) signal is connected with a slot (requestFinished(QNetworkReply *reply)) of this MyNetwork class.
When the button in app.cpp file is clicked , an object of MyNetwork class is created and inside its constructor network availability check is working.
also connect() method returns true but it never enters slot (requestFinished(QNetworkReply *reply)) method where i am handling my response.
mynetwork.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_ */
mynetwork.cpp
#include<mynetwork.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) {
if (reply->error() == QNetworkReply::NoError) {
qDebug() << "No Error";
} else {
int httpStatus = reply->attribute(
QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug() << "Error and the code is " << httpStatus;
}
reply->deleteLater();
} else {
qDebug() << "Reply comes out to be null";
}
}
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;
}
}
Solved! Go to Solution.
09-10-2012 10:32 AM
This superficially looks about right to me.
A few comments:
1. Do you get a reply object?
2. There is a window between getting the reply object and the connect where a background thread could finish before the connect is made. Try moving the connect to before the get. (I haven't tried this but I expect background threads are involved)
3. The global variable mNetworkMgr is misleading. Looks like you create an object of this type for each MyNetwork created and never release it. Better is to either make the a true member of MyNetwork; better yet is to just have it a variable in the sendRequest routine and set up appropriate connects to ensure it is eventually deleted. (Don't delete it too early)
Stuart
09-11-2012 07:23 AM - edited 09-11-2012 07:24 AM
Thanks for your reply.
1. Yes I am receiving the reply object but that the slot function is never invoked.
2. Yes connect() method was moved before the request (Get) invocation.
3. Well control never reaches the slot at all. How would the delete statement get executed earlier as in the slot the very first statement is qDebug() that's not printing at all.
I made the amendments you asked for but no change in the result. The slot is not being invoked.
One more thing that I am not running my project on simulator.
I am receiving this output :
Connection is success : ? : true
Reply from server is QNetworkReplyImpl(0x87b36b0)
09-11-2012 12:17 PM
Can you try to move
bool resFromServer = connect(mNetworkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*))); qDebug() << "Connection is success : ? : " << resFromServer;
before
QNetworkReply *reply = mNetworkMgr->get( QNetworkRequest( QUrl( "http://developer.blackberry.com/cascades/files/documentation/device_platform/networking/model.xml")));
You might have a network faster than the device/simulator your app was running on, e.g.: signal might have emitted before the slot got connected.
09-12-2012 01:10 AM
Yes I did. But no change in result.
09-12-2012 11:57 AM
hmm. I just did quick test for the you network code.
I moved
bool resFromServer = connect(mNetworkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*))); to be before QNetworkReply *reply = mNetworkMgr->get( QNetworkRequest( QUrl( "http://developer.blackberry.com/cascades/files/documentation/device_platform/networking/model.xml")));
in my test.
As I mentioned that we need to connect the signal before we trigger sending network request.
I got "No Error" in the log which seemed have indicated that the signal/slot was triggerred properly in my case.
Question for u:
how did u create your "MyNetwork" object?
is the object still alive when the network request signal is triggerred?
maybe the code segment for creating/destorying your "MyNetwork" object will be helpful
09-13-2012 02:03 AM
Yes you are right. The error was in instantiation of the MyNetwork object. That was a silly mistake, else code was correct.
Thanks linkBB10Dev for your reply.
11-22-2012 05:06 AM
Hi ,
Please tell me what was mistake . i want know because i am using your code but it is not working in my project
please help me
11-26-2012 02:23 AM
Hi Farhan,
I was creating MyNetwok object as
MyNetwork *object;
and due to this before background thread provide me response from server, the object was destroyed.
Correction :
MyNetwork *object=new MyNetwork();
Now this object will remain through out the app life.
This was error in my case.
02-27-2013 10:36 AM
Hello,
I have a question about your function isNetworkAvailable().
I'm using it to check if the network is available and it works great.
However, there is this one case when it doesn't give me the right answser.
Indeed, if, at first, I have network, isNetworkAvailable() will return true.
Then, if I switch off the Wifi, isNetworkAvailable() will return false which is OK.
But then, if I switch on the Wifi again, isNetworkAvailable() still returns false which is non OK this time.
Have you seen this problem ?
Thank you!