如何在GWT中使用AsyncCallback调用GET方法

时间:2015-08-06 09:58:25

标签: java json rest gwt

我有一个doGet(String url,最终的AsyncCallback回调)方法,用于从具有REST功能的服务器检索JSON,但我不知道如何在onModuleLoad()中调用它。代码是:

public void doGet(String url, final AsyncCallback<JavaScriptObject> callback) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {
        builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable caught) {
                callback.onFailure(caught);
            }
            public void onResponseReceived(Request request, Response response) {
                if(response.getStatusCode() == STATUS_CODE_OK) {
                    try {
                        callback.onSuccess(JsonUtils.safeEval(response.getText()));
                    } catch(IllegalArgumentException iax) {
                        callback.onFailure(iax);
                    }
                }
                else {
                    callback.onFailure(new Exception("Bad return code..."));
                }
            }
        });
    } catch(RequestException e) {
        callback.onFailure(e);
    }
}

我知道这是一个愚蠢的问题(我是初学者),但我怎么称呼doGet?例如:

doGet("http://192.168.1.10:8080/ProvaRpi/raspberry/sample/artisti", what here?);

我需要AsyncCallback,因为我想在另一个页面或TextBox中显示数据库中的JSON。 欢迎每一个代码改进! 谢谢!

1 个答案:

答案 0 :(得分:0)

你可以这样打电话:

doGet("your url", new AsyncCallback<JavaScriptObject>() {

    @Override
    public void onSuccess(JavaScriptObject result) {
        // process the result
    }

    @Override
    public void onFailure(Throwable caught) {
        // process when error
    }
});