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
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator
Accepted Solution

networking problems, QNetworkAccessManager?

[ Edited ]

Hello, I have this strange problem with networking and any help would be greately appreciated.

 

I have this code:

 

 MyCustomNetworkClass network;

	 // Set request parameters
	 QMap<QString, QString> params;
	 params.insert("username", userEmail);
	 params.insert("password", password);

	 network.initializeRequestParam(params);

	 network.makePostRequest("Login");

 Inside the MyCustomNetworkClass'es makePostRequest function, I call the following method:

mNetworkAccessManager->post(req, postData.encodedQuery()); 
// where "req" contains parameters set using the "initializeRequestParam" function as above. 

 and then in the requestFinished method of MyCustomNetworkClass, I am waiting for the response of the server:

void MyCustomNetworkClass::requestFinished(QNetworkReply* reply)
{
....	 // But it seems the program never gets here ??? Why ??
}

 Can anyone please spot where could be the problem??? ps. I manage to successfully connect signal and slot in the constructor of MyCustomNetworkClass, e.g., like this:

	bool result = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

 

Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: networking problems, QNetworkAccessManager?

I have solved this problem in a very strange way. The code I wrote before:

 

 MyCustomNetworkClass network;

	 // Set request parameters
	 QMap<QString, QString> params;
	 params.insert("username", userEmail);
	 params.insert("password", password);

	 network.initializeRequestParam(params);

	 network.makePostRequest("Login");

 I replaced with:

 

 // Network Manager object
	 MyCustomNetworkClass *network = new MyCustomNetworkClass();

	// Parameters for user login
	QMap<QString, QString> params;
	params.insert("username", userEmail);
	params.insert("password", password);
	params.insert("deviceId", QUuid::createUuid().toString());

	// Set parameters
	network->initializeRequestParam(params);

	// Issue post request to the server
	network->makePostRequest("Login");

 and now the requestFinished gets called. Why is this case??? Any ideas???

 

ps. Now I have a bit different problem, in the same place I add:

	bool res = QObject::connect(network, SIGNAL(signalSuccess(QVariantMap)), this, SLOT(successSlot(QVariantMap)));
	Q_ASSERT(res);
	Q_UNUSED(res);

 But this slot never gets called :smileysad:( ..

even though as I said the requestFinished now gets called successfully and it has this method implemented inside it, which should call the slot above:

 

		 emit signalSuccess(data);

 Any help?

 

Please use plain text.
Developer
scrudrv
Posts: 42
Registered: ‎11-23-2010
My Device: Q10
My Carrier: Rogers

Re: networking problems, QNetworkAccessManager?

Hey

 

The reason your new code works is because you dynamically created object of QNetworkManager. Which means that the object will exist until YOU delete it. In your 1st version the object was deleted automatically right after function finished it's work. The only thing I would add to your new implementation is don't forget to delete your "network" as soon as you don't need it.

 

To answer on the second part of your question I have to look at Slot implementation 1st.

 

Ok, I know the trend is old, but because I have my own issues with QNetworkManager and I still try to understand how does it work I decided to participate in solving this one.

 

Regards,

 

Please use plain text.