2013-03-24 125 views
1

我有一個與GWT不同尋常的問題。在編譯項目之後,在瀏覽器中運行.html文件來運行它。它運行良好,直到下面的代碼行顯示:GWT突然停止執行

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); 
} 

謝謝:)

+0

問題是? – 2013-03-24 16:12:22

+0

您對「請求」即異步調用的理解是有缺陷的。請閱讀https://developers.google.com/web-toolkit/doc/latest/tutorial/clientserver中的「進行異步調用」部分。 – SSR 2013-03-24 16:26:07

+0

http://stackoverflow.com/questions/10014319/code-after-gwt- RPC-asynccallbak - 將 - 不被執行的/ 10021925#10021925 – 2013-03-25 02:05:29

回答

2

對於GWT背景下理解AJAX - 請通過 「進行異步調用」 一節中https://developers.google.com/web-toolkit/doc/latest/tutorial/clientserver

你的編程閱讀getBoardSTickets()作爲執行異步請求調用後返回字符串的方法存在缺陷。不要試圖在getBoardStickets()中返回異步調用的結果。

return ticketsJSON.get(0);sendRequest()後立即被調用。由於RequestCallback()不會完成處理,它會拋出一個異常,因爲ticketsJSON將具有零個條目。

嘗試通過回調從外部

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

你調用代碼應更改爲

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

同樣的邏輯也適用於該調用異步調用服務器所有方法一樣。您不應編程以從方法返回請求響應的值。