我有一个doGet(String url,final AsyncCallback callback)方法,用于从具有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。
欢迎对每个代码进行改进!
谢谢!

最佳答案

您可以像这样拨打电话:

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

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

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

08-29 00:14