gpt4 book ai didi

json - 如何使用 GWT 处理 JSONP 回调?

转载 作者:行者123 更新时间:2023-12-02 08:47:23 25 4
gpt4 key购买 nike

我正在使用 GWT,我想发出一个 JSONP 请求,它会在返回时调用我的 GWT 方法。

但是,我无法弄清楚如何指定 GWT 方法以在回调时调用。谁能帮忙?这是我的示例代码:

private native void fetchUserData(String accessToken) /*-{
var callback = "com.company.example.FacebookApi::handleUser";
var url = "https://graph.facebook.com/me?access_token="+accessToken+"&callback=" + callback;

// use jsonp to call the graph
var script = document.createElement('script');
script.src = url;
document.body.appendChild(script);

}-*/;

public void handleUser(Object o) {
Window.alert("Received object with class: " + o.getClass().getName())
}

此代码移植自此示例:Facebook Without SDK .

或者,我刚刚发现有一个 GWT JsonpRequestBuilder 我还没有机会使用它,但如果有人可以在不使用任何 native 代码的情况下给出一个例子……那就更好了。

谢谢!

最佳答案

弄明白了,在很大程度上要感谢这些例子:

Gwt + JSONP

Cross Domain Requests with Gwt, Jsonp

Cross site referenceing in 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 响应中的 errorhometown 字段很容易包装到 JavaScriptObjects 中。

关于json - 如何使用 GWT 处理 JSONP 回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11440552/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com