用于Twitter搜索结果的GWT类型覆盖

时间:2010-01-17 01:10:53

标签: java gwt

我正在尝试调整GWT教程,以便在另一个站点上发出json数据请求,以便从twitter(http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html)中获取搜索结果。我无法弄清楚如何调整叠加类型,以适应推特结果:

{"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日编辑

这是其他代码。我window.alert上面的[3]号码会产生适当的提醒,例如(“页面说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; }-*/;
}

1 个答案:

答案 0 :(得分:0)

好的,明白了。

关键是要非常小心你的json对象/ javascript传递的java代码的类型。如果我没有将this.max_id强制转换为字符串,那么它会尝试传回javascript“number”类,而java不知道该怎么做。

这是工作覆盖类(带有额外的推文覆盖类)

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; }-*/;

}