How to call RESTFUL services from GWT?

Viewed 38001

I'm using GWT as web development framework. I need to access some REST services from my GWT client code. Also I need to parse JSON (or maybe XML) which is response format of these services. Which is the best way for this problem?

Thanks in advance.

6 Answers

For this stuff, I find it easier to fall back to using GWT JSNI.

Eg, Calling a JSON service to get the users country code:

public static native void getCountryCode(Loaded<String> countryCode) /*-{
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var jsonObj = JSON.parse(xhttp.responseText);
            countryCode.@mypackage.Loaded::data(*)(jsonObj.country_code);
        }
    };
    xhttp.open("GET", "https://api.ipdata.co/", true);
    xhttp.send();
}-*/;

Where "Loaded" is just:

package mypackage;

public interface Loaded<T> {
    public void data(T data);
}
Related