views:

182

answers:

1

imo i did everything according to the tutorial here Googles x-site

    /**
   * Make call to remote server.
   */
  public native static void getJson(int requestId, String url,
      StockWatcher handler) /*-{
   var callback = "callback" + requestId;

   // [1] Create a script element.
   var script = document.createElement("script");
   script.setAttribute("src", url+callback);
   script.setAttribute("type", "text/javascript");

   // [2] Define the callback function on the window object.
   window[callback] = function(jsonObj) {
   // [3]
     [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
     window[callback + "done"] = true;
   }

   // [4] JSON download has 1-second timeout.
   setTimeout(function() {
     if (!window[callback + "done"]) {
       [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
     }

     // [5] Cleanup. Remove script and callback elements.
     document.body.removeChild(script);
     delete window[callback];
     delete window[callback + "done"];
   }, 1000);

   // [6] Attach the script element to the document body.
   document.body.appendChild(script);
  }-*/;

but it keeps failing me.. all other methos are also written.. i just can understand why it tells me every time that "Couldn't retrieve JSON" (it tells that when handler's input is null)

btw i am talking about the "3. Requesting the data from the remote server" on the googles site

+3  A: 

I'd suggest using JsonpRequestBuilder, instead of all this JSNI code - no JSNI code (maybe just some overlay types), easier to debug, etc.

 String url = "http://www.google.com/calendar/feeds/[email protected]/public/full" +
     "?alt=json-in-script";
 JsonpRequestBuilder jsonp = new JsonpRequestBuilder(); // No JSNI!
 jsonp.requestObject(url,
     new AsyncCallback<Feed>() { // Type-safe!
       public void onFailure(Throwable throwable) {
         // Easy to debug! (hopefully)
       }

       public void onSuccess(Feed feed) {
         // Success!
         }
       }
     });
Igor Klimer
i tried it.for static files it worked, but in case of a dynamic json files it always failed.any ideas?
Are *you* responsible for generating the JSON files on the server side? Then maybe it's a bug :D (no offence ;)) Either way, try downloading the dynamic and static files via browser or wget and compare and validate them: http://www.jsonlint.com/ is a great JSON validator. Also: "it always failed" - could you be more specific as to how it "failed"? :D
Igor Klimer