views:

119

answers:

1

i'm doing a function in gwt
it sends an IQ stanza into a server and has to wait for the server answer
in the function i make the handler that waits for the answer from the server to that IQ stanza

so what i need is for the function to wait until i get the response from the server and after that do other stuff
i'm a beginner in gwt so any thoughts would be great
thanks

public void getServices()
    {
        IQ iq = new IQ(IQ.Type.get);
        iq.setAttribute("to", session.getDomainName());
        iq.addChild("query", "http://jabber.org/protocol/disco#items");

        session.addResponseHandler(iq, new ResponseHandler() 
        {
            public void onError(IQ iq, ErrorType errorType, ErrorCondition errorCondition, String text) 
            {
                <do stuff>
            }
            public void onResult(IQ iq) 
            {
                <do stuff>
            }
        });

        session.send(iq);

        <after receiving answer do stuff>   
    }
+1  A: 

You use the onResult method for that. Put all code that needs to be executed after the server response there.

Eric Eijkelenboom
but i need for the function getServices to return a boolean depending on the answer
hdantas
Well, the whole idea of asynchronous requests is that getServices might return BEFORE your service request is completed, thereby AVOIDING a wait :) Therefor, the result type of getServices cannot depend on the result of your service call. If you need to execute some code depending on the sucess/failure of the service call, you should include that check in your onResult/onError methods as well. Use those two methods to call the 'true and false branches' of your code.
Eric Eijkelenboom
@hdantas - You should go through this amazing 'beer in the fridge' analogy - http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/faca1575f306ba0f.
sri
@sri - Nice one :)
Eric Eijkelenboom
@Eric i was trying to find a way so that could be done, i found a way now, thanks for the explanation
hdantas
@sri lolnice analogy :)
hdantas