Android Facebook sdk,发布供稿文字和图片

时间:2015-01-18 21:22:30

标签: android facebook facebook-graph-api post feed

我有代码。我登录facebook并获取用户详细信息,但我需要在用户墙上发布一些Feed。 我不知道该怎么做。请帮忙。是不是可以不创建片段?我只想按按钮发布故事。不是Facebook登录按钮。

 public class FaceBookLogin {
        private Context mContext;
        private MyActivity mActivity;
        private SessionTracker mSessionTracker;
        private PersonResponse mPersonResponse;
        public Session mCurrentSession;
        private boolean pendingPublishReauthorization = false;


    public FaceBookLogin(Context context, MyActivity activity) {
        mContext = context;
        mActivity = activity;
        mPersonResponse = new PersonResponse();
    }
/*login facebook*/
    public void FbLogin() {
        mSessionTracker = new SessionTracker(mContext, new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {

            }
        }, null, false);

        mCurrentSession = mSessionTracker.getSession();

        if(mCurrentSession == null || mCurrentSession.getState().isClosed()) {
            mSessionTracker.setSession(null);
            Session session = new Session.Builder(mContext).setApplicationId(Constants.APP_ID).build();
            Session.setActiveSession(session);
            mCurrentSession = session;
        }

        if(!mCurrentSession.isOpened()) {
            Session.OpenRequest openRequest = null;
            openRequest = new Session.OpenRequest(mActivity);
            if(openRequest != null) {
                openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS);
                openRequest.setPermissions(Constants.PERMISSIONS);
                openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
                mCurrentSession.openForRead(openRequest);

            }
        } else {
            requestUserData();
        }
        mCurrentSession.addCallback(new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                if(session.isOpened()) {
                    Toast.makeText(mActivity, "session.isOpened", Toast.LENGTH_SHORT).show();
                } else if(session.isClosed()) {
                    Toast.makeText(mActivity, "session.isClosed()", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
//get user details
    private void requestUserData() {
        if(mCurrentSession.isOpened()) {
            Request.executeMeRequestAsync(mCurrentSession, new Request.GraphUserCallback() {
                @Override
                public void onCompleted(GraphUser user, Response response) {
                    String name = "";
                    String surName = "";
                    int sex = 0;
                    Long dateOfBirth = Long.valueOf(0);
                    String city = "";
                    String photo = "";

                    JSONObject response_json = user.getInnerJSONObject();
                    try {

                        if(!response_json.isNull(Constants.FB_FIELD_FIRST_NAME)) {
                            name = response_json.getString(Constants.FB_FIELD_FIRST_NAME);
                        }
                        if(!response_json.isNull(Constants.FB_FIELD_LAST_NAME)) {
                            surName = response_json.getString(Constants.FB_FIELD_LAST_NAME);
                        }
                        //This person's birthday in the format MM/DD/YYYY
                        if(!response_json.isNull(Constants.FB_FIELD_BIRTHDAY)) {
                            String date = response_json.getString(Constants.FB_FIELD_BIRTHDAY);
                            date = date.replaceAll("\\/", "");
                            dateOfBirth = Long.valueOf(date).longValue();
                        }
                        if(!response_json.isNull(Constants.FB_FIELD_GENDER)) {
                            sex = response_json.getInt(Constants.FB_FIELD_GENDER);
                        }
                        if(!response_json.isNull(Constants.FB_FIELD_HOMETOWN)) {
                            city = response_json.getJSONObject(Constants.FB_FIELD_HOMETOWN).getString(Constants
                                    .FB_FIELD_CITYNAME);
                        }
                        if(!response_json.isNull(Constants.FB_FIELD_ID)) {
                            //tip: this link will redirect
                            photo = "http://graph.facebook.com/" + response_json.getString(Constants.FB_FIELD_ID) +
                                    "/picture?type=large";
                        }

                        mPersonResponse.setName(name);
                        mPersonResponse.setSurname(surName);
                        mPersonResponse.setSex(sex);
                        mPersonResponse.setDateOfBirth(dateOfBirth);
                        mPersonResponse.setCity(city);
                        mPersonResponse.setPhoto(photo);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }


}

0 个答案:

没有答案