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

Handle server response

[ Edited ]

Hi,

 

I have an app which has a separate networking class MyNetwork say that uses QNetworkAccessManager.

 

Now, in my app, when user clicks some UI controls, server requests need to be made.

For example, if user clicked button1 I want to invoke MyAPICall1 and if user clicked button 2

I want to invoke MyAPICall2. What I want to achieve is that if say user clicked button 1

I want some action to happen on the UI (e.g., display list using server response), and in case of button 2 I want different action to happen

on UI (e.g., open other dialog with server response). What are the ways to achieve this? ps. As I mentioned I also need to use the response from both API calls in my UI code.

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Handle server response

you can use a single network manager for multiple methods.
just connect the onFinished signal to the appropriate slot for your method call (and disconnect it afterwards)

I connect the result signal from qml and trigger the network, in the onResult function in qml i disconnect the signal and handle the data.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Handle server response

[ Edited ]

Hi, imagine I have following connections:

 

	connect(mNetworkAccessManagerObject1, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

and

	connect(mNetworkAccessManagerObject2, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

 will different invocations of requestFinished take place for  mNetworkAccessManagerObject1 and mNetworkAccessManagerObject2 ??

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Handle server response

Hi,

 

They both will call the same method but sender() function can be used to find the sender of the message:

QNetworkAccessManager *manager = (QNetworkAccessManager *) sender();
if (manager == mNetworkAccessManagerObject1)
{
...
}

But I think it's better to use one instance of QNetworkAccessManager. It can handle multiple requests simultaneously.

 

QNetworkReply has finished() signal as well. QNetworkReplies finished() signals can be connected to different slots.

 

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Handle server response

[ Edited ]

Hi, as I mentioned before I have following situation.

I have say MyNetworkClass. Where it is possible to issue calls like this:

 

MyNetworkClass *networkManager = new MyNetworkClass();

networkManager->makePostRequest("ApiCallLogin");
// When the above method finishes (and thus finished() signal
//is called in the QNetworkAccessManager object),
//I want to be able to conduct some UI events. Say:
MyButton button = new MyButton();
button.SetText(server.response.text);
MyList list1 = new MyList();
list.updateWithItems(server.response.items);

// Now at some different point in time, I may need to call
// a different API function. Which looks like below.
networkManager->makePostRequest("ApiCallSendDetails");

// similarlty to the previous case, however different in
// terms of actions, I want to be able also to issue
// certain UI comands, e.g., 
MyContainer container = new MyContainer();
MyTree tree = new MyTree();
container.add(tree);
tree.initWithObject(response.objects).
 

ps. During different API calls of course different type of data may come form the server and I want to be able to handle all cases. how to achieve this? thanks.

 

Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Handle server response

Just use a different slot for each type of processing you want to do.

I also find it much easier to connect to the finished() signal on the QNetworkReply, rather than on the network manager

i.e.

QNetworkReply* reply1 = makePostRequest("ApiCallLogin")

reply->connect(SIGNAL(finished()), this, SLOT(customProcessing1()));


QNetworkReply* reply2 = makePostRequest("ApiCallSomethingElse")

reply2->connect(SIGNAL(finished()), this, SLOT(customProcessing2()));

Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Handle server response

[ Edited ]

Hi, thanks for your reply. But, when the finished() signal will be emitted, will not it call both customProcessing1 and customProcessing2 slots together?? This I may not want... Smiley Sad( because at some point, I may want to do some UI operation based on finished() signal, and at other point in time, on the same signal, I may want to do different UI operation... what to do in this case?? Thank you.

Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Handle server response

only if you tell it to
Developer
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Handle server response

[ Edited ]

Yes, exactly I may not want that. What I want instead is say I have one signal and two slots:

	
At one point, when signalSuceeded is emitted, I want the InvokeSomeSLot1 to be invoked like this:
1. bool res = QObject::connect(network, SIGNAL(signalSuceeded(QString)), this, SLOT(InvokeSomeSlot1(QString)));

Afterwards, I may want during that same signal, another Slot to be invoked (there is no need to invoke "InvokeSomeSlot1" once again as it was invoked previously). 

2.	bool res = QObject::connect(network, SIGNAL(signalSuceeded(QString)), this, SLOT(InvokeSomeSlot2(QString)));

But what will the above line (2) do?? Will not it invoke also InvokeSomeSlot1 and InvokeSomeSlot2?? and that I may not want as I said; I want only InvokeSomeSlot2 to be invoked but they use the same signal ... 

Maybe I should use different "network" objects for each signal - slot connection and that will solve my issue??? Thanks.

 

Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: Handle server response

You are connecting two slots to the same signal - of course it is going to call both of them

Read my first reply again. Connect to the finished() signal on each QNetworkReply you create - NOT the QNetworkAccessManager