如下面一段代码
public class MyApplication implements EntryPoint {
public void onModuleLoad() { // define the service you want to call
MyServiceAsync svc = (MyServiceAsync) GWT.create(MyService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) svc;
String svUrl = GWT.getModuleBaseURL() + "myService";
endpoint.setServiceEntryPoint(svUrl); // define a handler for what to do when the
// service returns a result
AsyncCallback callback = new AsyncCallback() {
public void onSuccess(Object result) {
RootPanel.get().add(new HTML(result.toString()));
} public void onFailure(Throwable ex) {
RootPanel.get().add(new HTML(ex.toString()));
}
}; // execute the service
svc.myMethod(1, callback); }
}
请问什么时候调用onSuccess,和onFailure方法?
public interface AnotherCallback { public void onFailure(ClassA a,Throwable t); public void onSuccess(ClassA a,Object rst);
}
请问什么时候调用onSuccess,和onFailure方法?
调用异步接口之后,一方面,程序会通过异步接口,去访问后台的impl(实现同步接口的servlet);另一方面,还会继续执行svc.myMethod(1, callback)之后的语句,即Window.alert("Async interface has been called.")。也就是说,当你运行时,如果考虑往咯时延,browse页面中会先弹出“Async interface has been called.”提示框,然后才是
onSuccess中代码的执行。public class MyApplication implements EntryPoint {
/**入口点*/
public void onModuleLoad() {
//异步接口初始化:同步接口转化为异步接口
MyServiceAsync svc = (MyServiceAsync) GWT.create(MyService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) svc;
String svUrl = GWT.getModuleBaseURL() + "myService";
endpoint.setServiceEntryPoint(svUrl);
//实例化回调函数类
AsyncCallback callback = new ResultAsyncCallback();
//调用异步接口
svc.myMethod(1, callback);
//Debug
Window.alert("Async interface has been called.");
}
/**回调函数类。用于获取和处理异步接口返回的结果*/
class ResultAsyncCallback implements AsyncCallback{
public void onSuccess(Object result) {
RootPanel.get().add(new HTML(result.toString()));
}
public void onFailure(Throwable ex) {
RootPanel.get().add(new HTML(ex.toString()));
}
}
}
我是问什么时候调用onSuccess和onFailure 方法
当类型为Object类型或null时调用onSuccess方法,为Throwable类型时调用onFailure方法。
不知道对不对。