开发者

GWT JsonpRequestBuilder Timeout issue

I am getting time out from using JsonpRequestBuilder.

The entry point code goes like this:

// private static final String SERVER_URL = "http://localhost:8094/data/view/";
private static final String SERVER_URL = "http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json-in-script&callback=insertAgenda&orderby=starttime&max-results=15&singl开发者_开发百科eevents=true&sortorder=ascending&futureevents=true";
private static final String SERVER_ERROR = "An error occurred while "
        + "attempting to contact the server. Please check your network "
        + "connection and try again.";

/**
 * This is the entry point method.
 */
public void onModuleLoad() {

    JsonpRequestBuilder requestBuilder = new JsonpRequestBuilder();
    // requestBuilder.setTimeout(10000);
    requestBuilder.requestObject(SERVER_URL, new Jazz10RequestCallback());

}

class Jazz10RequestCallback implements AsyncCallback<Article> {


    @Override
    public void onFailure(Throwable caught) {
        Window.alert("Failed to send the message: " + caught.getMessage());     
    }

    @Override
    public void onSuccess(Article result) {
        // TODO Auto-generated method stub
        Window.alert(result.toString());
    }

The article class is simply:

import com.google.gwt.core.client.JavaScriptObject;

public class Article extends JavaScriptObject {


    protected Article() {};


}

The gwt page, however, always hit the onFailure() callback and show this alert:

Failed to send the message. Timeout while calling <url>.

Fail to see anything on the Eclipse plugin console. I tried the url and it works perfectly.

Would appreciate any tip on debugging technique or suggestion


Maybe you should set the callback function explicitly via setCallbackParam, since you have callback=insertAgenda in your url - I presume that informs the server what should be the name of the callback function that wraps the JSON. Also, it's worth checking Firebug's console (or a similar tool for your browser) - even if GWT doesn't report any exceptions, Firebug still might.

PS: It's useful to use a tool like Firebug to see if the application does in fact receive the response from the server (that would mean that, for example, you do need the setCallbackParam call) or maybe there's something wrong on the server side (for whatever reason).


You have to read the callback request-Parameter (default callback, value something like __gwt_jsonp__.P0.onSuccess) on serversite and have to modify the output to

<callback>(<json>);

In this case:

__gwt_jsonp__.P0.onSuccess(<json>);


Both of these guys are absolutely correct, but here is a concrete example to help you understand exactly what they are referring too.

This is a public JSON api. Take a look at the results:

http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&country=GB&maxRows=4


This public API supports JSONP through the predefined parameter 'callback'. Basically whatever value you pass into callback, will be used as the function name to wrap around the JSON data you desire. Take a look at the results of these few requests:

http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&country=GB&maxRows=4&callback=totallyMadeUp

http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&country=GB&maxRows=4&callback=trollingWithJSONP


It could be happening because of another reason, that the webservice call is returning a JSON object and but the callback is expecting JSONP object (note there is a difference).

So if you are dealing with google maps api, and you are seeing this exception, you need to change it to api provide by maps api, something like

    final GeocoderRequest request = GeocoderRequest.create();
    request.setAddress(query);
    try {
        GWT.log("sending GeoCoderRequest");
        if (m_geocoder == null) {
            m_geocoder = Geocoder.create();
        }

        m_geocoder.geocode(request, new Geocoder.Callback() {
            @Override
            public void handle(final JsArray<GeocoderResult> results,
                    final GeocoderStatus status) {
                handleSuccess(results, status);
            }
        });
    } catch (final Exception ex) {
        GWT.log("GeoCoder", ex);
    }

Or else you could use RequestBuilder as in gwt library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜