Return type of AsyncCallback methods

102 views
Skip to first unread message

Maddy

unread,
Jun 10, 2009, 1:29:40 PM6/10/09
to Google Web Toolkit
Hi,

How can I change the return type of Asynccallback methods from void to
'Request'. Some where in the forums, I read that we can change the
return type to 'Request' object. But When I tried, It is giving me
compilation error. Please let me know, how this can be done.

Thanks
Maddy

Dalla

unread,
Jun 10, 2009, 2:15:33 PM6/10/09
to Google Web Toolkit
Simply put, you don´t, because the call itself won´t return anything.

The "return type" is defined inside the AsyncCallback.
If you have an interface like so:

public interface GreetingServiceAsync {
void greetServer(String input, AsyncCallback<String> callback);
}

String will be your "return type".


Then you make a call to your service, and when it returns (hopefully
successful)
you can handle the String in the onSuccess event handler.

greetingService.greetServer(textToServer, new AsyncCallback<String>()
{
public void onFailure(Throwable caught) {
//
Handle failure
}

public void onSuccess(String result) {
//Do
some stuff with result
}
});

What you need to do is basically change AsyncCallback<String> to
AsyncCallback<Request>.
Also change the return type for your syncronous service to Request.

Isaac Truett

unread,
Jun 10, 2009, 2:23:39 PM6/10/09
to Google-We...@googlegroups.com
I think you might be importing the wrong Request class. The one you
want is com.google.gwt.http.client.Request. Is that the class you are
importing?

Could probably tell you more if you provided the compilation error. :)

Ian Petersen

unread,
Jun 10, 2009, 2:24:53 PM6/10/09
to Google-We...@googlegroups.com
On Wed, Jun 10, 2009 at 11:15 AM, Dalla<dalla_...@hotmail.com> wrote:
> Simply put, you don´t, because the call itself won´t return anything.

That's not actually true. I've never done what Maddy is suggesting
myself, so, sorry Maddy, I can't directly answer your question except
by suggesting you search the forum history because it has been covered
before. I do know, though, that you can change the async methods to
return something meaningful and doing so will give the caller control
over the underlying XMLHttpRequest that will be used to make the RPC.
It gives you the opportunity to modify request headers, inspect
response headers, cancel inflight requests, modify timeouts, etc.

The rest of your message was spot on, though--the return value for the
RPC itself _is_ passed as an argument to the AsyncCallback's onSuccess
method. What Maddy's looking for is a handle on the RPC action
itself, as opposed to the value returned by the RPC.

Ian

Maddy

unread,
Jun 10, 2009, 2:29:35 PM6/10/09
to Google Web Toolkit
Hi,

I am using GWT1.4.61 and I guess, I can not use generics syntax.

Even in the above example, If I change the return type from String to
'Request'. How can I get the string value, which is actually the
response. I need the 'request' object only to handle the state of
that.

Thanks
Maddy

Maddy

unread,
Jun 10, 2009, 2:32:40 PM6/10/09
to Google Web Toolkit
Yes. I need the Handle on the RPC request, so that I can cancel, if
that is pending.

Jason Essington

unread,
Jun 10, 2009, 3:29:52 PM6/10/09
to Google-We...@googlegroups.com
it isn't the callback methods, but rather the return type of the
method in your Async Interface

so rather than
void doSomething(AsyncCallback cb);

it would be
Request doSomething(AsyncCallback cb);

Though I believe that is only available from GWT 1.5 on

-jason

Maddy

unread,
Jun 10, 2009, 5:36:02 PM6/10/09
to Google Web Toolkit
Thanks Jason,

It is not supported in GWT 1.4.61

Thanks
Maddy

Damon Lundin

unread,
Jun 11, 2009, 10:54:42 AM6/11/09
to Google Web Toolkit
You can also have your async method return RequestBuilder which will
not dispatch the RPC call, but rather returns the RequestBuilder
already set up and ready to go. You can then make changes to it
before making the call such as setting a timeout. Then you dispatch
the RPC call which will return you the Request object you ask for
above.

RequestBuilder builder = service.myMethod(new AsyncCallback
() {...});
builder.setTimeoutMillis(REQUEST_TIMEOUT);
Request request = builder.send();
Reply all
Reply to author
Forward
0 new messages