如何解决在GWT中等待RPC响应?

时间:2013-07-18 14:11:11

标签: gwt rpc

我知道RPC是异步的,但是如何以其他方式(除了等待响应)解决这个问题:

public static String htsl(String sentence)
{
    final DataBaseAsync db = GWT.create(DataBase.class);
    String cookie = staticContent.getCookie("ll");
    String shortcut = cookie.split("/")[1];
    final String[] lala = new String[1];
    database.getTranslated(sentence, shortcut, new AsyncCallback<String>() {



        @Override
        public void onSuccess(String result) {
            lala[0]=result;

            //this result I want to return in static function htsl... ?

        }

        @Override
        public void onFailure(Throwable caught) {
            // TODO Auto-generated method stub

        }


    });
    //here I have blank result, because RPC is slower than te return.. 
    And i have always blank result
    return lala[0];

}

我知道这不好,但是..如果没有解决方案,如何等待回复?

2 个答案:

答案 0 :(得分:1)

使htsl异步(非阻塞,即使用响应的callack而不是返回值)

答案 1 :(得分:0)

这样做:

public static String htsl(String sentence, AsyncCallback<String> myCallback) {
    final DataBaseAsync db = GWT.create(DataBase.class);
    String cookie = staticContent.getCookie("ll");
    String shortcut = cookie.split("/")[1];
    final String[] lala = new String[1];

    database.getTranslated(sentence, shortcut, myCallback);
}

然后你让其他类实现AsyncCallback,将它作为myCallback方法传递给htsl方法,鲍勃是你的叔叔。