android facebook帖子在墙上不工作

时间:2014-01-06 08:09:44

标签: android facebook android-facebook

我在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;
}

1 个答案:

答案 0 :(得分:1)

这是一个非常常见的错误。问题是requestNewPublishPermissions是一个异步请求。该方法立即返回,但直到稍后(在提示用户之后)您才会实际获得权限。这里发生的是你立即发出发布请求,但你还没有获得权限。

您需要做的是在请求新权限时保存状态,当您获得已获得新权限的回调时,请尝试该请求。

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