我正在嘗試改編GWT教程來請求另一個站點上的json數據從twitter獲取搜索結果(http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html)。我想不通但是如何適應疊加類型,以適應Twitter的結果它來作爲:GWT爲twitter搜索結果提供類型覆蓋
{"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)
所以結果是,需要通過讀取某種對象的數組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; }-*/;
public final native String getToUser() /*-{ return this.to_user_id; }-*/;
}
我試着寫getResults()
爲return this.results[0].to_user_id;
(只是爲了看看我是否可以讀取內部的領域之一,但不工作)。
僅供參考我想讀的數據是這樣的:
/**
* 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 = asArrayOfResultData(tweets);
displayError(results.get(0).getResults());
}
感謝您的幫助。在2010年1月17日
編輯:
這是額外的代碼。我打電話給[3]
的window.alert
會產生正確的警報,例如, (「的網頁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]
[email protected]::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; }-*/;
}
你如何獲得'JavaScriptObject tweets'參數? 'this.results [0] .to_user_id' *應該*指向正確的東西,所以也許你通過錯誤的JSO? (或「以錯誤的方式」創建它)一些更多的源代碼會有所幫助:) – 2010-01-17 15:32:21
我已更新我的帖子,更多代碼。謝謝! – 2010-01-17 19:17:46