GWT RPC机制如何使用非void返回类型

时间:2011-08-31 07:13:12

标签: gwt rpc

我有一个场景,我需要为Synchrnous函数指定一个返回类型,代码如下:

@RemoteServiceRelativePath("show_box")
public interface ShowBoxCommandService extends RemoteService{

 public ArrayList<String> showBox();

}

服务器上方法的实现是:

public ArrayList<String> showBox() {

  ArrayList<String> box = new ArrayList<String>();
  Iterator<Box> boxes = BoxRegistry.getInstance().getBoxes();
  while (boxes.hasNext()) {
     box.add(boxes.next().toString());
  }
  return box;
}

我试图在客户端以下列格式定义回调变量,以便调用方法

 AsyncCallback<Void> callback = new AsyncCallback<Void>() {
           public void onFailure(Throwable caught) {
              // TODO: Do something with errors.
              // console was not started properly
           }

           @Override
           public void onSuccess(Void result) {
              // TODO Auto-generated method stub
              // dialog saying that the console is started succesfully

           }
        };

使用aync接口代码进行更新:

    public interface ShowBoxCommandServiceAsync {

   void showBox(AsyncCallback<ArrayList<String>> callback);

  }

但这导致Async方法中方法的定义发生变化。

任何想法或线索都会有所帮助。

谢谢, Bhavya

P.S。如果这是重复,请道歉

4 个答案:

答案 0 :(得分:1)

如果服务界面如下所示:

public interface ShowBoxCommandService extends RemoteService {
  public ArrayList<String> showBox();
}

然后你必须有一个关联的异步接口:

public interface ShowBoxCommandServiceAsync {
  public void showBox(AsyncCallback<ArrayList<String>> callback);
}

这意味着,您应传递给showBox的回调类型为AsyncCallback<ArrayList<String>>

new AsyncCallback<ArrayList<String>>() {

  @Override
  public void onSuccess(ArrayList<String> list) {
    // ...
  }

  @Override
  public void onFailure(Throwable caught) {
    // ...
  }
}

答案 1 :(得分:1)

回调应该是:

AsyncCallback<ArrayList<String>> callback = new AsyncCallback<ArrayList<String>>() {
       public void onFailure(Throwable caught) {
          // TODO: Do something with errors.
          // console was not started properly
       }

       @Override
       public void onSuccess(ArrayList<String> result) {
          // TODO Auto-generated method stub
          // dialog saying that the console is started succesfully

       }
    };

如果您不需要使用结果,那么您可以忽略它,但如果是这种情况,您可能会质疑您的设计以及为什么需要该方法在第一个中返回ArrayList<String>的地方。

答案 2 :(得分:0)

咦?您的方法返回一个ArrayList,并且您在调用中声明无效?

Change <Void> to <ArrayList<String>>

答案 3 :(得分:0)

你的回调不应该是虚空。如果您的同步方法返回字符串列表,则异步回调方法应该接收列表。您必须使用ArrayList,因为该类需要实现Serializable接口。

AsyncCallback<ArrayList<String>> callback = new AsyncCallback<ArrayList<String>>() {
   public void onFailure(Throwable caught) {
      // TODO: Do something with errors.
      // console was not started properly
   }

   @Override
   public void onSuccess(ArrayList<String> result) {
      // TODO Auto-generated method stub
      // dialog saying that the console is started succesfully
   }
};