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
Accepted Solution

Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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/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) { 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; } }

 

 

Always Success
Please use plain text.
BlackBerry Development Advisor (Retired)
smacmartin
Posts: 499
Registered: ‎05-07-2012
My Device: developer

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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

Please use plain text.
Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

[ Edited ]

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)

 

 

 

Always Success
Please use plain text.
BlackBerry Development Advisor
lingBB10Dev
Posts: 35
Registered: ‎08-01-2012
My Device: BB10 alpha-BB10 developer

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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.

 

 

 

 

Please use plain text.
Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

Yes I did. But no change in result.

Always Success
Please use plain text.
BlackBerry Development Advisor
lingBB10Dev
Posts: 35
Registered: ‎08-01-2012
My Device: BB10 alpha-BB10 developer

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Please use plain text.
Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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.

 

Always Success
Please use plain text.
Regular Contributor
mohdfarhanakram
Posts: 73
Registered: ‎12-30-2011
My Device: All devices

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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

 

Thanks

Mohd Farhan
Please use plain text.
Developer
MFSI
Posts: 80
Registered: ‎09-03-2012
My Device: 9810

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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.

Always Success
Please use plain text.
Contributor
nico411
Posts: 34
Registered: ‎12-20-2010
My Device: DevAlpha Beta 2

Re: Error while establishing a Http connection. Once request finished signal is emitted, no invocation of slot

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!

Please use plain text.