How to call RESTFUL services from GWT?

前端 未结 6 1187
小蘑菇
小蘑菇 2020-12-08 10:59

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 o

6条回答
  •  醉话见心
    2020-12-08 10:59

    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 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 {
        public void data(T data);
    }
    

提交回复
热议问题