Facebook SDK作为单独的类发布

时间:2013-09-05 08:46:09

标签: android facebook

我使用示例代码

中的以下方法创建了一个类
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(TAG,
                            "JSON error "+ e.getMessage());
                    }
                    FacebookRequestError error = response.getError();
                    if (error != null) {
                        Toast.makeText(getActivity()
                             .getApplicationContext(),
                             error.getErrorMessage(),
                             Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity()
                                 .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;
    }

我有时会在权限行上收到以下错误 -

( session.requestNewPublishPermissions(newPermissionsRequest);)
  

java.lang.UnsupportedOperationException:Session:尝试为具有待处理请求的会话请求新权限。

本课程中我所拥有的只是上述方法。当我想向Facebook发布内容时,我称之为。 Login的代码位于不同的类中,未调用。

问题确实是我收到此错误的原因?我是否需要在课程中添加一些UiLifecycleHelper代码,如果是,那么哪些部分?

想把它作为一个单独的类,但是我走错了路线,应该只构建到正在进行发布的Activity类

感谢您的时间

0 个答案:

没有答案