title: GWT with type overlay for twitter search results
body: <p>I am trying to adapt the GWT tutorial for making requests for json data on another site to grab search results from twitter (<a href="http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html" rel="nofollow noreferrer">http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html</a>).  I cannot figure out how to adapt the overlay type however to fit the twitter results which come as:</p>
  
  <pre><code>{"results":[
   {"text":"@twitterapi  http:\/\/tinyurl.com\/ctrefg",
   "to_user_id":396524,
   "to_user":"TwitterAPI",
   "from_user":"jkoum",
   "id":1478555574,   
   "from_user_id":1833773,
   ...
  </code></pre>
  
  <p>(<a href="http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search" rel="nofollow noreferrer">http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search</a>)</p>
  
  <p>So results is an array of objects of some sort that need to be read by the java program.  I was trying to write an overlay class, but I couldn't figure out how to parse the results array bit because I'm not really sure what the type is supposed to be. </p>
  
  <pre><code>package com.google.twentyNumbers.client;
  
  import com.google.gwt.core.client.JavaScriptObject;
  
  public class TwitterResults extends JavaScriptObject {
  
      protected TwitterResults() { }
  
      public final native String getResults() /*-{ return this.results; }-*/;
      public final native String getToUser() /*-{ return this.to_user_id; }-*/;
  }
  </code></pre>
  
  <p>I tried writing <code>getResults()</code> as <code>return this.results[0].to_user_id;</code> (just to see if I could read one of the inner fields, but that does not work).</p>
  
  <p>For reference I am trying to read the data this way:</p>
  
  <pre><code>     /**
    * Cast JavaScriptObject as JsArray of StockData.
    */
   private final native JsArray&lt;TwitterResults&gt; asArrayOfResultData(JavaScriptObject jso) /*-{
      return jso;
   }-*/;
  
  
  /**
    * Handle the response to the request for stock data from a remote server.
   */
   private void handleJsonResponse(JavaScriptObject tweets) {
      if (tweets == null) {
        displayError("Couldn't retrieve JSON");
        return;
      }
  
      JsArray&lt;TwitterResults&gt; results = asArrayOfResultData(tweets);
      displayError(results.get(0).getResults());
  
   }
  </code></pre>
  
  <p>Thanks for any help.</p>
  
  <p><strong><em>Edit on 1/17/2010</strong>:</em> </p>
  
  <p>Here is additional code.  The <code>window.alert</code> I call above <code>[3]</code> produces the proper alert e.g. ("The page says 787304</p>
  
  <pre><code>/**
  * Cast JavaScriptObject to class TwitterResults
  */
  private final native TwitterResults asArrayOfResultData(JavaScriptObject jso) /*-{
   return jso;
  }-*/;
  
  
  /**
     * Handle the response to the request for twitter data from a remote server.
     */
    private void handleJsonResponse(JavaScriptObject tweets) {
      /*if (tweets == null) {
        displayError("Couldn't retrieve JSON");
        return;
      }*/
  
      TwitterResults tw = asArrayOfResultData(tweets);
  
      displayError(tw.getMaxId()); // here it is 'null'
    }
  
  
  /**
     * Make call to remote server.
     */
    public native static void getJson(int requestId, String url, TwentyNumbers 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) {
      window.alert(jsonObj.max_id); // here it alerts the correct value
     // [3]
       handler.@com.google.twentyNumbers.client.TwentyNumbers::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
       window[callback + "done"] = true;
     }
  ...
  
  /* File: TwitterResults.java */
  
  package com.google.twentyNumbers.client;
  
  import com.google.gwt.core.client.JavaScriptObject;
  
  public class TwitterResults extends JavaScriptObject {
  
      protected TwitterResults() { }
  
      //public final native String getResults() /*-{ return this.results[0].to_user_id; }-*/;
      public final native String getSinceId() /*-{ return this.since_id; }-*/;
      public final native String getMaxId() /*-{ return this.max_id; }-*/;
      //public final native String getToUser() /*-{ return this.to_user_id; }-*/;
  }
  </code></pre>