GWT with type overlay for twitter search results
I am trying to adapt the GWT tutorial for making requests for json data on another site to grab search results from twitter (http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html). I cannot figure out how to adapt the overlay type however to fit the twitter results which come as:
{"results":[
{"text":"@twitterapi http:\/\/tinyurl.com\/ctrefg",
"to_user_id":396524,
"to_user":"TwitterAPI",
"from_user":"jkoum",
"id":1478555574,
"from_user_id":1833773,
...
(http://apiwiki.twitter.com/Twitter-Search-API-Method%3A-search)
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.
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; }-*/;
}
I tried writing getResults()
as return this.results[0].to_user_id;
(just to see if I could read one of the inner fields, but that does not work).
For reference I am trying to read the data this way:
/**
* Cast JavaScriptObject as JsArray of StockData.
*/
private final native JsArray<TwitterResults> 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<TwitterResults> results = asArrayO开发者_Go百科fResultData(tweets);
displayError(results.get(0).getResults());
}
Thanks for any help.
Edit on 1/17/2010:
Here is additional code. The window.alert
I call above [3]
produces the proper alert e.g. ("The page says 787304
/**
* 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; }-*/;
}
Okay, got it.
The key is to be VERY careful about what type your java code is being handed from the json object / javascript. If I don't cast this.max_id
to a string then it attempts to pass back the javascript "number" class which java has no idea what to do with.
Here is the working overlay class (with an additional tweet overlay class)
package com.google.twentyNumbers.client;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
public class TwitterResults extends JavaScriptObject {
protected TwitterResults() {
}
//public final String getMaxId() { return "500"; }
public final native JsArray<Tweet> getResults() /*-{ return this.results; }-*/;
public final native String getMaxId() /*-{ return ''+this.max_id; }-*/;
public final native String getRefreshUrl() /*-{ return this.refresh_url; }-*/;
public final native String getNextPage() /*-{ return this.next_page; }-*/;
}
精彩评论