2010-03-08 30 views
5

我從使用JsonpRequestBuilder中獲取時間。GWT JsonpRequestBuilder超時問題

入口點代碼是這樣的:

// private static final String SERVER_URL = "http://localhost:8094/data/view/"; 
private static final String SERVER_URL = "http://www.google.com/calendar/feeds/[email protected]/public/full?alt=json-in-script&callback=insertAgenda&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending&futureevents=true"; 
private static final String SERVER_ERROR = "An error occurred while " 
     + "attempting to contact the server. Please check your network " 
     + "connection and try again."; 

/** 
* This is the entry point method. 
*/ 
public void onModuleLoad() { 

    JsonpRequestBuilder requestBuilder = new JsonpRequestBuilder(); 
    // requestBuilder.setTimeout(10000); 
    requestBuilder.requestObject(SERVER_URL, new Jazz10RequestCallback()); 

} 

class Jazz10RequestCallback implements AsyncCallback<Article> { 


    @Override 
    public void onFailure(Throwable caught) { 
     Window.alert("Failed to send the message: " + caught.getMessage());  
    } 

    @Override 
    public void onSuccess(Article result) { 
     // TODO Auto-generated method stub 
     Window.alert(result.toString()); 
    } 

文章類很簡單:

import com.google.gwt.core.client.JavaScriptObject; 

public class Article extends JavaScriptObject { 


    protected Article() {}; 


} 

GWT的網頁,然而,總是打onFailure處()的回調,並顯示此警報:

Failed to send the message. Timeout while calling <url>. 

未能在Eclipse插件控制檯上看到任何內容。我嘗試了網址,它完美的作品。

希望任何提示上調試技術或建議

回答

4

也許你應該通過setCallbackParam明確設置的回調函數,因爲你必須在你的URL callback=insertAgenda - 我相信,通知應該是什麼回調的名稱服務器函數包裝JSON。 另外,值得檢查Firebug的控制檯(或者瀏覽器的類似工具) - 即使GWT不報告任何異常,Firebug仍然可能。 PS:使用類似Firebug的工具來查看應用程序是否確實收到來自服務器的響應(這意味着,例如,您確實需要撥打setCallbackParam),或者可能有問題,這很有用。服務器端(無論什麼原因)。

0

您必須在serversite閱讀回調請求參數(默認callback,有價值的東西像__gwt_jsonp__.P0.onSuccess),並具有輸出修改到

<callback>(<json>); 

在這種情況下:

__gwt_jsonp__.P0.onSuccess(<json>); 
0

兩個這些傢伙是絕對正確的,但這裏有一個具體的例子來幫助你明確他們所指的是什麼。

這是一個公共JSON API。看看結果:

http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&country=GB&maxRows=4


這個公共API,可以通過預定義的參數「回調」支持JSONP。基本上,無論您傳遞給回調函數的任何值都將用作函數名稱來包裝所需的JSON數據。看看這幾個要求的結果:

http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&country=GB&maxRows=4&callback=totallyMadeUp

http://ws.geonames.org/postalCodeLookupJSON?postalcode=M1&country=GB&maxRows=4&callback=trollingWithJSONP

0

這可能會發生,因爲另一個原因,即Web服務調用返回一個JSON對象,但回調期待JSONP對象(注意是有區別的)。

所以,如果你是在處理與谷歌地圖API,並且看到此異常,則需要將其更改爲API的地圖API提供,像

final GeocoderRequest request = GeocoderRequest.create(); 
    request.setAddress(query); 
    try { 
     GWT.log("sending GeoCoderRequest"); 
     if (m_geocoder == null) { 
      m_geocoder = Geocoder.create(); 
     } 

     m_geocoder.geocode(request, new Geocoder.Callback() { 
      @Override 
      public void handle(final JsArray<GeocoderResult> results, 
        final GeocoderStatus status) { 
       handleSuccess(results, status); 
      } 
     }); 
    } catch (final Exception ex) { 
     GWT.log("GeoCoder", ex); 
    } 

否則,您可以使用RequestBuilder作爲gwt庫。