想通了,这在很大程度上要归功于这些例子:
重量 + JSONP
使用 Gwt、Jsonp 的跨域请求
GWT 中的跨站点引用
这是根据评论更新的代码(未指定回调,使用 Javascript 覆盖类型)
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;
}-*/;
}
为了完整起见,这是 JSO 包装的原始 JSON 响应。由于继承,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
}
请注意,JSON 响应中的error
和hometown
字段很容易包装到 JavaScriptObjects 中。