GWT RPC机制如何使用非void返回类型
我有一个场景,需要为synchrous函数指定一个返回类型,代码如下:GWT RPC机制如何使用非void返回类型,gwt,rpc,Gwt,Rpc,我有一个场景,需要为synchrous函数指定一个返回类型,代码如下: @RemoteServiceRelativePath("show_box") public interface ShowBoxCommandService extends RemoteService{ public ArrayList<String> showBox(); } @RemoteServiceRelativePath(“显示框”) 公共接口ShowBoxCommandService扩展了Rem
@RemoteServiceRelativePath("show_box")
public interface ShowBoxCommandService extends RemoteService{
public ArrayList<String> showBox();
}
@RemoteServiceRelativePath(“显示框”)
公共接口ShowBoxCommandService扩展了RemoteService{
公共ArrayList 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;
}
public ArrayList showBox(){
ArrayList box=新的ArrayList();
迭代器框=BoxRegistry.getInstance().getBox();
while(box.hasNext()){
添加(box.next().toString());
}
返回框;
}
我试图在客户端以以下格式定义回调变量,以便调用该方法
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
}
};
AsyncCallback callback=new AsyncCallback(){
失败时的公共无效(可丢弃){
//TODO:处理错误。
//控制台未正确启动
}
@凌驾
成功时公开作废(作废结果){
//TODO自动生成的方法存根
//表示控制台已成功启动的对话框
}
};
使用aync接口代码更新:
public interface ShowBoxCommandServiceAsync {
void showBox(AsyncCallback<ArrayList<String>> callback);
}
公共接口ShowBoxCommandServiceAsync{
无效显示框(异步回调);
}
但这会导致异步方法中方法的定义发生更改
任何想法或线索都会有帮助
谢谢,
巴维亚
如果这是重复的话,请道歉,嗯?你的方法返回一个ArrayList,你在调用中声明void
Change <Void> to <ArrayList<String>>
更改为
您的回调不应无效。如果同步方法返回字符串列表,则异步回调方法应接收该列表。您必须使用ArrayList,因为该类需要实现可序列化接口
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
}
};
AsyncCallback callback=new AsyncCallback(){
失败时的公共无效(可丢弃){
//TODO:处理错误。
//控制台未正确启动
}
@凌驾
成功时公共无效(ArrayList结果){
//TODO自动生成的方法存根
//表示控制台已成功启动的对话框
}
};
如果服务界面如下所示:
public interface ShowBoxCommandService extends RemoteService {
public ArrayList<String> showBox();
}
回调应该是:
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
}
};
AsyncCallback callback=new AsyncCallback(){
失败时的公共无效(可丢弃){
//TODO:处理错误。
//控制台未正确启动
}
@凌驾
成功时公共无效(ArrayList结果){
//TODO自动生成的方法存根
//表示控制台已成功启动的对话框
}
};
如果不需要使用结果,则可以忽略它,但如果是这种情况,则可能应该质疑您的设计,以及为什么首先需要该方法返回
ArrayList
。该方法将返回ArrayList,而不是ArrayList@lonut当使用上述情况调用show box方法时,我在IDE中得到一个错误,要求我将异步接口的返回类型更改为ArrayList,我确实记得读到异步接口的返回类型应该为void@BhavyaPH,你能用异步接口的源代码更新这个问题吗?如果返回类型为void,IDE不应该抱怨。顺便问一下,什么IDE?Eclipse与谷歌的插件?@lonut非常感谢你的评论,我已经用code@Bhavya我不知道。一切看起来都很好。可能是IDE有问题。如果我不使用void,aync调用的返回类型将从void更改为ArrayList,这不应该是这样,有什么线索吗?坦白说,我不明白你在说什么。如果您想要字符串列表,请返回该列表;如果您想要返回void,请不要返回任何内容。
new AsyncCallback<ArrayList<String>>() {
@Override
public void onSuccess(ArrayList<String> list) {
// ...
}
@Override
public void onFailure(Throwable caught) {
// ...
}
}
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
}
};