获得发布许可后,facebook发布Feed粘贴在黑屏上

时间:2013-03-19 14:12:25

标签: android facebook facebook-android-sdk

好的,所以我想在我的用户的墙上贴一个新帖子。我已按照此链接https://developers.facebook.com/docs/howtos/androidsdk/3.0/publish-to-feed/

上的developers.facebook提供的代码

在发布故事方法中,我添加一个日志以查看程序是否已通过代码行:

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

if (session != null){

    Log.d("INFO","session is not NULL");

    // Check for publish permissions    
    List<String> permissions = session.getPermissions();
    if (!isSubsetOf(PERMISSIONS, permissions)) {

         Log.d("INFO","setting permission");

        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");

    Log.d("INFO","finish construct message");

    Request.Callback callback= new Request.Callback() {
        public void onCompleted(Response response) {

            Log.d("INFO","on request complete!");

            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();
}

}

问题是,当我执行上面的方法时,程序运行良好,直到它弹出一个验证发布权限窗口并且我允许它,屏幕再次继续到黑屏,其上有不确定的进度条,然后窗口只是自己解雇。

实际上,从logcat我可以得出结论它没有通过“on request complete”行。

为什么会这样?任何线索?

谢谢!

2 个答案:

答案 0 :(得分:2)

哦是的,我目前正在遵循此解决方案Updated - Android Facebook api 3.0 error: Cannot call LoginActivity with a null calling package

像你说的那样,我已经添加了像这样的onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

它有效!虽然我在对话类

上安排了这些行
public void createFacebookConnection() {
        Session session = new Session(MapScreen.this);
        Session.setActiveSession(session);

        Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);

        Session.StatusCallback statusCallback = new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                String message = "Facebook session status changed - " + session.getState() + " - Exception: " + exception;
                Log.w("Facebook test", message);

                if (session.isOpened() || session.getPermissions().contains("publish_actions")) {
                    publishToWall();
                } else if (session.isOpened()) {
                    OpenRequest open = new OpenRequest(MapScreen.this).setCallback(this);
                    List<String> permission = new ArrayList<String>();
                    permission.add("publish_actions");
                    open.setPermissions(permission);
                    Log.w("Facebook test", "Open for publish");
                    session.openForPublish(open);
                }
            }
        };

        if (!session.isOpened() && !session.isClosed() && session.getState() != SessionState.OPENING) {
            session.openForRead(new Session.OpenRequest(MapScreen.this).setCallback(statusCallback));
        } else {
            Log.w("Facebook test", "Open active session");
            Session.openActiveSession(MapScreen.this, true, statusCallback);
        }
    }

    void publishToWall() {
        Session session = Session.getActiveSession();

        Bundle postParams = new Bundle();
        postParams.putString("name", "Merauke Android");
        postParams.putString("caption", hotelName);
        postParams.putString("description", hotelName+" is a luxury hotel which is located in the city of Bandung.");
        postParams.putString("link", "https://developers.facebook.com/android");
        postParams.putString("picture", "http://www.jejaringhotel.com/android/hotelimages/hotel-3.jpg");
        Log.i("DIALOG","feed set");

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

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

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }
非常感谢你的建议李明!谢谢!

答案 1 :(得分:0)