Reputation: 435
I have an unusual issue with GWT. I run it with oppening the .html file after compiling the project, on my browser. It is running fine until the following lines of code appear:
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);
}
};
}
This method is called by this other method:
private static Button addBoardButton(String name) {
Button button = new Button(name);
button.addClickHandler(HandlerManager.addBoardButtonHandler(name));
return button;
}
Which is also running properly. Here is the getBoardSTickets() method:
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);
}
Thank you :)
Upvotes: 1
Views: 124
Reputation: 9537
For understanding ajax in GWT context - Please read through "Making Asynchronous Call" section in https://developers.google.com/web-toolkit/doc/latest/tutorial/clientserver
Your programming of getBoardSTickets() as a method that returns string after executing a asynchronous request call is flawed. Do not try to return the result of a asynchronous call in getBoardSTickets().
return ticketsJSON.get(0);
gets invoked immediately after sendRequest()
. It would throw a exception as ticketsJSON would have zero entries because RequestCallback() would not have completed processing.
Try passing a callback from outside
protected static String getBoardSTickets(String name, RequestCallback callback){
//Code for making request
}
You invocation code should change to
getBoardSTickets(name, new RequestCallback(){
//onSuccess and onFailure handling.
} )
The same logic holds true for all methods that invoke a async call to server. You should not program to return a value of the request response from the method.
Upvotes: 2