想通了,由於在很大程度上這些例子:
Gwt + JSONP
Cross Domain Requests with Gwt, Jsonp
Cross site referenceing in GWT
下面是更新後的代碼,每個註釋(沒有指定的回調,使用Javascript Overlay Type)
private void fetchDataUsingGwt() {
String url = "https://graph.facebook.com/me?access_token=" + accessToken;
JsonpRequestBuilder requestBuilder = new JsonpRequestBuilder();
requestBuilder.requestObject(url, new AsyncCallback<FbUser>() {
@Override
public void onFailure(Throwable caught) {
Window.alert(caught.getMessage());
}
@Override
public void onSuccess(FbUser fbUser) {
if (fbUser.isError()) {
StringBuilder builder = new StringBuilder();
builder.append("Fb error: ");
builder.append(fbUser.getError().getMessage() + ", ");
builder.append(fbUser.getError().getCode());
String message = builder.toString();
Window.alert(message);
return;
}
StringBuilder builder = new StringBuilder();
builder.append("Fetched user: " + fbUser.getFirstName() + " " + fbUser.getLastName());
builder.append(" from " + fbUser.getHometown().getName());
builder.append(" born on " + fbUser.getBirthday());
builder.append(" with id " + fbUser.getId() + " and email " + fbUser.getEmail());
builder.toString();
String details = builder.toString();
Window.alert("Got: " + details);
}
});
}
,並且響應使用JSO像這樣自動換:
public class FbError extends JavaScriptObject {
protected FbError() {
}
public final native String getMessage() /*-{
return this.message;
}-*/;
public final native String getType() /*-{
return this.type;
}-*/;
public final native String getCode() /*-{
return this.code;
}-*/;
public final native String getSubCode() /*-{
return this.error_subcode;
}-*/;
}
public class Hometown extends JavaScriptObject {
protected Hometown() {
}
public final native String getName() /*-{
return this.name;
}-*/;
public final native String getId() /*-{
return this.id;
}-*/;
}
public class ErrorableJso extends JavaScriptObject {
public boolean isError() {
return getError() != null;
}
public final native FbError getError() /*-{
return this.error;
}-*/;
}
public class FbUser extends ErrorableJso {
// TODO: Separate call needed to retrieve profile pic
protected FbUser() {
}
public final native String getFirstName() /*-{
return this.first_name;
}-*/;
public final native String getLastName() /*-{
return this.last_name;
}-*/;
public final native String getId() /*-{
return this.id;
}-*/;
public final native String getBirthday() /*-{
return this.birthday;
}-*/;
public final native String getEmail() /*-{
return this.email;
}-*/;
public final native Hometown getHometown() /*-{
return this.hometown;
}-*/;
}
爲了完整性,這是原始JSON響應JSO包裝。因繼承,使用相同的FbUser
對象,如果有任何像這樣的錯誤:
{
"error": {
"message": "Error validating access token: Session has expired at unix time 1342044000. The current unix time is 1342050026.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463
}
}
或者預期的用戶對象:
{
"id": "23232323",
"name": "Andrew Cuga",
"first_name": "Andrew",
"last_name": "Cuga",
"link": "http://www.facebook.com/TheAndy",
"username": "TheAndy",
"birthday": "02/20/2011",
"hometown": {
"id": "108530542504412",
"name": "Newark, Delaware"
} // ... etc
}
注意error
和hometown
領域的JSON響應是輕鬆包裝成JavaScriptObjects。
您可能還會考慮使其更通用,並且無需任何手動轉換即可返回任何[Javascript Overlay Type](https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsOverlay)。這樣,當你開始使用更多的API方法時,不會導致代碼重複太多。 – 2012-07-11 22:39:24
requestBuilder.setCallbackParam(「callback」); 是不必要的,這是默認設置。我也同意Chris的觀點,覆蓋類型會讓你的生活更輕鬆,代碼更乾淨。 – LINEMAN78 2012-07-11 22:44:20
感謝你們倆。我很快就知道這個回調並不是必需的,然後把它拿出來。我現在正忙着重疊 - 謝謝! – Cuga 2012-07-11 23:36:21