How to call a rest web service from gwt mobile application

711 views
Skip to first unread message

syruss

unread,
Jun 21, 2011, 2:53:43 AM6/21/11
to Google Web Toolkit
Hi all,

I have created a mobile application using GWT.I have successfully
created the UI part but i have to call a rest web service from GWT mob
app.

What i have tried is
1.I have called the rest web service using HttpUrlConnection in java
its working fine as web app but if i deployed the app in mobile its
not working.
2.I have tried to call the service using RequestBuilder but its giving
0 status code.

Please suggest me to proceed that

Thanks in advance

Juan Pablo Gardella

unread,
Jun 21, 2011, 1:14:01 PM6/21/11
to google-we...@googlegroups.com
See http://restygwt.fusesource.org/

2011/6/21 syruss <navink...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Jens

unread,
Jun 21, 2011, 2:01:30 PM6/21/11
to google-we...@googlegroups.com
Is the web service you want to call accessible on the same protocol://domain:port as your GWT app? Normally RequestBuilder should work fine for requesting rest web services but maybe you have been run into the Same Origin Policy? If thats the case I think you have to use GWT's JsonpRequestBuilder to access the external web service.

-- J.

syruss

unread,
Jun 23, 2011, 5:38:53 AM6/23/11
to Google Web Toolkit
Thanks juan

On Jun 21, 10:14 pm, Juan Pablo Gardella <gardellajuanpa...@gmail.com>
wrote:
> Seehttp://restygwt.fusesource.org/
>
> 2011/6/21 syruss <navinkumar...@gmail.com>

syruss

unread,
Jun 23, 2011, 5:42:24 AM6/23/11
to Google Web Toolkit




Hi jens,


I have used JsonpRequestBuilder to call external service .We can hit
the server but while getting the response we are getting timeout
Exception
Here is the code :



JsonpRequestBuilder jsonRequestBuilder=new JsonpRequestBuilder();
jsonRequestBuilder.setTimeout(10000);
jsonRequestBuilder.setCallbackParam("JSONCALLBACKPARAM");
String callbackParam=jsonRequestBuilder.getCallbackParam();
System.out.println("JSONCALLBACKPARAM : " + callbackParam);
jsonRequestBuilder.requestObject("http://localhost:8081/LMS-WB/
services/authenticate/saranraju/welcome1/", new AsyncCallback() {

@Override
public void onFailure(Throwable caught) {

System.out.println("StackTrace : "+caught.getStackTrace());
System.out.println(caught.getClass().getName());
System.out.println("I caught in Failure Statte :
"+caught.getCause());
}

@Override
public void onSuccess(Object result) {
System.out.println(result.hashCode());
System.out.println(result.getClass().getName());
}
});


can you suggest me what we have missed out.

Thanks in advance





On Jun 21, 11:01 pm, Jens <jens.nehlme...@gmail.com> wrote:
> Is the web service you want to call accessible on the same
> protocol://domain:port as your GWT app? Normally RequestBuilder should work
> fine for requesting rest web services but maybe you have been run into the Same
> Origin Policy <http://en.wikipedia.org/wiki/Same_origin_policy>? If thats

Jens

unread,
Jun 23, 2011, 7:03:41 AM6/23/11
to google-we...@googlegroups.com
Does the webservice evaluate your "JSONCALLBACKPARAM" request parameter correctly and sends back the correct "<jsoncallbackparam value>(<your json data>)" string? 

-- J.

navin kumar

unread,
Jun 23, 2011, 7:19:01 AM6/23/11
to google-we...@googlegroups.com
hi Jens ,


Thanks a lot for your reply


If i hit the server from the above code i got the server log which
states that it processed the request successfully but while getting
the response i got this this error

I have written the rest service which send this as response
{"UIComponent":"org.lms.valueObjects.PresentationVO@ae3da8"}

com.google.gwt.jsonp.client.TimeoutException


should i change any thing in server side ?

Thanks in advance

Jens

unread,
Jun 23, 2011, 9:43:16 AM6/23/11
to google-we...@googlegroups.com
Basically JSONP uses the HTML script tag to load data from a different domain (to avoid the same origin policy. It does not apply for script tags). 

That means that if you do a request to "http://localhost:8081/LMS-WB/services/authenticate/saranraju/welcome1/" via JsonpRequestBuilder GWT will insert something like:

<scriptype="text/javascript" src="http://localhost:8081/LMS-WB/services/authenticate/saranraju/welcome1/?callback=<javascript methodname>">

into the web page.

Note the "?callback=<javascript methodname>" portion of the URL. Your webservice has to evaluate the "callback" parameter and has to use the supplied method name for building the response. Instead of returning pure JSON you have to return Javascript (as the request is done via a script tag) and the correct return value would be "methodname({ ... JSON DATA ... })". 
Before doing the request GWT will add a JavaScript function to your web page and the name of that function will be send to your server via the callback parameter. That function will be called when your request is done and that function will call your onSuccess method.

As you have called jsonRequestBuilder.setCallbackParam("JSONCALLBACKPARAM"); you would have to evaluate the parameter "JSONCALLBACKPARAM" on your server and use its value to construct the return value as described above.

Hope that helps.

-- J.

navin kumar

unread,
Jun 27, 2011, 7:03:33 AM6/27/11
to google-we...@googlegroups.com
Thanks a lot jens .

can u provide a sample code how to do that ..

if i try to get the callback param in server side im getting null.

Will that param send as callback=functionname?

please suggest me

Thank in advance

navin kumar

unread,
Jun 27, 2011, 8:00:25 AM6/27/11
to google-we...@googlegroups.com
Hi jens

I have tried the steps what you have given in the last post.

I have modified the server code to check for the callback function name

http://localhost:8081/LMS-WB/services/authenticate/saranraju/welcome1/?callback=jsoncallback


if i hit the url server will send response as
"jsoncallback({org.lms.valueObjects.PresentationVO@17e60a1})"

but if i tried to access callback param through gwt app i got null .

am i missed out any thing ?

am i sending the response correctly?( i have get the object in ui and
manipulate the result )

please help me out ..

navin kumar

unread,
Jun 27, 2011, 10:53:40 AM6/27/11
to google-we...@googlegroups.com
thanks a lot jens i have successfully received the response fron the server.

The response is javascriptobject of gwt.

Is it possible to manipulate the script object if so please let me know the way

thanks in advance

Reply all
Reply to author
Forward
0 new messages