gpt4 book ai didi

java - GWT 突然停止执行

转载 作者:可可西里 更新时间:2023-11-01 17:10:56 24 4
gpt4 key购买 nike

我有一个关于 GWT 的异常问题。在我的浏览器上编译项目后,我通过打开 .html 文件来运行它。它运行良好,直到出现以下代码行:

public static ClickHandler addBoardButtonHandler(final String name) {
return new ClickHandler(){

@Override
public void onClick(ClickEvent event) {
Window.alert("We will retrieve them!"); //this line runs
String boardSTickets = getBoardSTickets(name); // this too
Window.alert("We got tickets!"); // the code is never executing this line
String boardSSwimlanes = getBoardSSwimlanes(name);
Window.alert("We got swimlanes!");
KanbanizerClient.showSingleBoard(boardSTickets, boardSSwimlanes);
}

};
}

这个方法被另一个方法调用:

private static Button addBoardButton(String name) {
Button button = new Button(name);
button.addClickHandler(HandlerManager.addBoardButtonHandler(name));
return button;
}

这也正常运行。这是 getBoardSTickets() 方法:

protected static String getBoardSTickets(String name) {
final List<String> ticketsJSON = new LinkedList<String>();
try {
Request request = Builder.createBuilder(RequestBuilder.GET, "http://localhost:8080/Kanbanizer/boards/" + name + "/tickets").sendRequest(null, new RequestCallback(){

@Override
public void onResponseReceived(Request request,
Response response) {
if(response.getStatusCode() == 200){
ticketsJSON.add(response.getText());
}

}

@Override
public void onError(Request request, Throwable exception) {
// TODO Auto-generated method stub

}

});
} catch (RequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ticketsJSON.get(0);
}

谢谢你:)

最佳答案

要了解 GWT 上下文中的 ajax - 请通读 https://developers.google.com/web-toolkit/doc/latest/tutorial/clientserver 中的“进行异步调用”部分

您将 getBoardSTickets() 编程为在执行异步请求调用后返回字符串的方法是有缺陷的。不要尝试在 getBoardSTickets() 中返回异步调用的结果。

return ticketsJSON.get(0);sendRequest() 之后立即被调用。它会抛出异常,因为 ticketsJSON 的条目为零,因为 RequestCallback() 不会完成处理。

尝试从外部传递一个回调

protected static String getBoardSTickets(String name,  RequestCallback callback){
//Code for making request
}

您的调用代码应更改为

getBoardSTickets(name, new RequestCallback(){
//onSuccess and onFailure handling.
} )

同样的逻辑适用于调用异步调用服务器的所有方法。您不应编程为从该方法返回请求响应的值。

关于java - GWT 突然停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15600590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com