【问题标题】:android facebook post on wall not working墙上的android facebook帖子不起作用
【发布时间】:2014-01-07 02:20:11
【问题描述】:

我在 Facebook 墙上发帖时遇到了一个问题。我使用 facebook SDK,我的代码如下。

下面的代码询问:myApp 你想代表你发布。但是我的 fb 墙上没有发布任何内容。请给我建议,我如何在 Facebook 墙上发帖。我想从我的应用程序中发布静态文本。例如 String message = "Hello all";将通过我的应用发布在我的墙上。

    private void publishStory() {
    Session session = Session.getActiveSession();

    if (session != null){

        // Check for publish permissions    
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session
                    .NewPermissionsRequest(this, PERMISSIONS);
        session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

        Bundle postParams = new Bundle();
        postParams.putString("name", "Facebook SDK for Android");
        postParams.putString("caption", "Build great social apps and get more installs.");
        postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
        postParams.putString("link", "https://developers.facebook.com/android");
        postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");

        Request.Callback callback= new Request.Callback() {
            public void onCompleted(Response response) {
                JSONObject graphResponse = response
                                           .getGraphObject()
                                           .getInnerJSONObject();
                String postId = null;
                try {
                    postId = graphResponse.getString("id");
                } catch (JSONException e) {
                    Log.i("HERE",
                        "JSON error "+ e.getMessage());
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(getApplicationContext(),
                         error.getErrorMessage(),
                         Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), 
                             postId,
                             Toast.LENGTH_LONG).show();
                }
            }
        };

        Request request = new Request(session, "me/feed", postParams, 
                              HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }

}

private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}

【问题讨论】:

    标签: android facebook android-facebook


    【解决方案1】:

    这是一个很常见的错误。问题是 requestNewPublishPermissions 是一个异步请求。该方法会立即返回,但直到稍后(提示用户之后)您才会真正获得权限。这里发生的情况是您正在立即发出发布请求,但您尚未获得权限。

    你需要做的是在你请求新权限时保存状态,当你收到你已经获得新权限的回调时,再尝试请求。

    查看演示此策略的 HelloFacebook 示例应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多