如何使用GWT处理JSONP回调?

时间:2012-07-11 20:03:33

标签: json gwt jsonp

我正在使用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 我还没有机会使用它,但是如果有人可以在不使用任何本机代码的情况下给出一个例子......那就更好了。

谢谢!

1 个答案:

答案 0 :(得分:6)

想出来,非常感谢这些例子:

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中。