在GWT异步回调中,如何在onSuccess中处理相同的返回类型?

时间:2018-04-17 11:35:30

标签: java gwt rpc

如果我在服务中有两个boolean方法,我怎样才能确保onSuccess方法处理返回boolean的正确方法?

例如,在onSuccess方法中我说:

if (result instanceof Boolean) {};

有没有办法可以区分返回布尔值的服务方法?否则,我无法保证在我的onSuccess方法中执行正确的代码,就好像结果是boolean,如果调用了我的两个boolean服务方法中的任何一个,它将会执行。

以下是我遇到的问题的一个例子:

private class DefaultCallback implements AsyncCallback
        {
        @Override
        public void onFailure(Throwable caught)
            {
            mainGUI.getServerResponseLabel().setStyleName("serverResponseLabelError");
            mainGUI.getServerResponseLabel().setHTML("An error occurred while "
                    + "attempting to contact the server. Please check your network " + "connection and try again.");
            }

        @Override
        public void onSuccess(Object result)
            {
            if (result instanceof Boolean)
                { 
                //
                I have two methods that return a boolean here, 
                so this block will execute no matter which one is called;
                for one method I want to display a GUI windows saying "Upload complete",          
                and for another, create an excel spreadsheet. But the boolean value of one method won't be relevant to the other

1 个答案:

答案 0 :(得分:3)

有很多灰色区域,但请耐心等待: 在SomethingRPCService.java中

boolean isA();
boolean isB();
在SomethingRPCServiceAsync.java中

void isA(AsyncCallback<Boolean> callback);
void isB(AsyncCallback<Boolean> callback);

在您的活动中

somethingService.isA(new AsyncCallback<Boolean>() {
        @Override
        public void onSuccessImpl(final Boolean response) {
            //will be executed on success of THIS call
        }
        @Override
        public void onFailure(final Throwable caught) {
            // not relevant here
        }
});

somethingService.isB(new AsyncCallback<Boolean>() {
        @Override
        public void onSuccessImpl(final Boolean response) {
            //will be executed on success of THIS call
        }
        @Override
        public void onFailure(final Throwable caught) {
            // not relevant here
        }
});

你可以(必须?)实际输入你的回报

编辑:用你的例子,我可以看到更好一点;你的回调不应该是相同的,你应该覆盖它,所以代码不一样,因为它不是