-1

我知道 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];

}

我知道这不好,但是..如果这没有可行的解决方案,如何等待响应?

4

2 回答 2

1

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

于 2013-07-18T14:13:32.363 回答
0

Do it like so:

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);
}

Then you let some other class implement AsyncCallback, pass it as the myCallback argument to the htsl method and Bob's your uncle.

于 2013-07-18T15:01:30.707 回答