2010-12-13 33 views
0

我有一個項目,您可以請求由jax-rs以json格式提供的資源。當我查詢json出現的其他URL時,瀏覽器中的所有內容都能正常工作。GWT和REST(jax-rs)

現在我想要我的GWT項目來請求這些資源並處理它們並在我的界面中顯示它們。我發現最簡單的方法是使用請求生成器和覆蓋。代碼較低。問題是,代碼運行時似乎永遠不會進入實際的RequestCallback()。狀態字符串從不改變。我認爲這可能是一個SOP,所以我補充了但仍然沒有奏效。任何理想?

package com.workoutcell.client; 
//import com.google.gwt.core.client.JavaScriptObject; 
import com.google.gwt.core.client.JsArray; 
import com.google.gwt.http.client.*; 
import com.google.gwt.http.client.Request; 
import com.google.gwt.http.client.RequestBuilder; 
import com.google.gwt.http.client.RequestCallback; 
import com.google.gwt.http.client.RequestException; 
import com.google.gwt.http.client.Response; 

/** 
* 
* @author 
*/ 

public class RestToInfoSession{ 

    String queryReturn = null; 
    JsArray<InfoJSO> arrayOfInfo = null; 
    String host = "http://localhost:8080/mysite"; 
    String restModule = "/calendar/getinfo"; 
    String id = null; 
    String year = null; 
    String month = null; 
    String status = "Not Initialized"; 

    public RestToInfoSession(String id, String year, String month){ 

     this.id =id; 
     this.year = year; 
     this.month = month; 
     String url = host + restModule + "/"+this.id + "/"+this.year + "/"+this.month; 
     RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url); 


     try { 
      status = "Initialized at Url " + builder.getUrl(); 
      Request request = builder.sendRequest(null, new RequestCallback() { 

       public void onError(Request request, Throwable exception) { 
        // Couldn't connect to server (could be timeout, SOP violation, etc.) 
        status = "Error on connecting to Server"; 
       } 


       public void onResponseReceived(Request request, Response response) { 
        if (200 == response.getStatusCode()) { 
         // arrayOfInfo = jsonToJsArray(response.getText());  
         status = "JSON has been Fetched. Result is:" + response.getText(); 
        } else if(0 == response.getStatusCode()) { 
         status = "Error is 0"; 
        } else { 
         status = "Error in JSON Request:" + response.getStatusCode(); 
         //response.getStatusText(); 
        } 
       } 

      }); 

     } catch (RequestException ex) { 
      status = "Error in Request Builder Startup"; 
     } 

    } 

    //get an jso object in array 
    private final native JsArray<InfoJSO> jsonToJsArray(String json) /*-{ 
    return eval(json); 
     }-*/; 

    public JsArray<InfoJSO> getInfoArray(){ 

     return arrayOfInfo; 
    } 

} 

UPDATE:我的問題是一樣的。我Referring to a non-final variable data inside an inner class意識到wasnt工作機制異步調用的。我仍然不知道如何傳遞我的response.getText()來更新不屬於我的RestToInfoSession類的標籤....任何想法?

+0

如果我使用google chrome developper工具,我可以看到我的javascript獲得了200個代碼,並且收到的結果是我的json字符串。但結果並未顯示在標籤中的瀏覽器應用程序中。 這就是我的入口點放入的東西: Label lab3 = new Label(); (新的RestToInfoSession(「2」,「2010」,「12」)。getJsonText()); – guiomie 2011-01-09 14:20:12

回答

0

我已經把計時器每隔1000ms檢查一次,如果我的json字符串已經從null更新到xhttp請求的數據。這有效,但我有一種感覺,有一種解決這個問題的更優雅的方式。

1

考慮使用RestyGWT項目。它將使調用JAXRS JSON資源像使用GWT-RPC一樣簡單。另外,您通常可以在客戶端重新使用來自服務器端的相同請求響應DTO。