如何使用android SDK将支票发布到facebook

时间:2014-01-16 04:14:50

标签: facebook facebook-android-sdk android-facebook

文档非常糟糕。我想发布用户登录Facebook。根据创建checkin对象的文档不推荐使用

https://developers.facebook.com/docs/reference/api/checkin

而你应该添加一个附有位置数据的帖子。这就是我想要做的。或者也许我应该尝试发布和打开图形故事?

无论如何,这就是我所拥有的,它基本上是发布SDK示例中的帖子的代码,帖子已创建,但没有附加位置数据。

 private void publishStory() {

    Session session = Session.getActiveSession();

    if (session != null) {
        Bundle placeBundle = new Bundle();
        Bundle locationBundle = new Bundle();
        Bundle postParams = new Bundle();

        locationBundle.putString("latitude",String.valueOf(place.getLat()));
        locationBundle.putString("longitude",String.valueOf(place.getLng()));

        placeBundle.putString("id", place.getPage_id());
        placeBundle.putString("name", place.getName());
        placeBundle.putBundle("location", locationBundle);

        postParams.putBundle("place", placeBundle);
        postParams.putString("message", "test message");

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

                String postId = null;

                try {

                JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();


                    postId = graphResponse.getString("id");
                }
                catch (JSONException e) {
                    Log.i(app.TAG, "JSON error " + e.getMessage());
                }
                catch(Exception e){
                    e.printStackTrace();
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(mContext, error.getErrorMessage(), Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(mContext, postId, Toast.LENGTH_LONG).show();
                }
            }
        };

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

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    }
    else {
        toast("no session to publish");
    }

}

会话确实具有发布权限,它将发布帖子,但所有内容都是“测试消息”字符串。 place对象来自facebook的服务器,因此它是一个带有page_id的实际位置。当我调试post params看起来像这样

 Bundle[{message=test message, place=Bundle[{id=171229079554355, location=Bundle[{longitude=-122.434568, latitude=37.797314}], name=The Brixton San Francisco}]}]

1 个答案:

答案 0 :(得分:2)

      Session.openActiveSession(activity, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session != null && session.isOpened()) {

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

                Bundle postParams = new Bundle();
                postParams.putString("message", message);

                    postParams.putString("tags",tag);
                         postParams.putString("place",place_id);



                Request.Callback callback = new Request.Callback() {
                    private String toastmessage;

                    public void onCompleted(Response response) {
                        try {
                            JSONObject graphResponse = response
                                    .getGraphObject().getInnerJSONObject();
                            String postId = null;

                            postId = graphResponse.getString("id");
                        } catch (Exception e) {
                            Log.i("Test", "JSON error " + e.getMessage());
                        }
                        FacebookRequestError error = response.getError();
                        if (error != null) {
                            isPosted(false);
                            Toast.makeText(
                                    activity.getApplicationContext(),
                                    error.getErrorMessage(),
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            isPosted(true);
                            toastmessage = "Posted Successfully";
                            Toast.makeText(activity, toastmessage,
                                    Toast.LENGTH_SHORT).show();
                            {

                            }
                        }
                    }
                };

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

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


        }
    });