首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android应用程序Facebook Checkin

Android应用程序Facebook Checkin
EN

Stack Overflow用户
提问于 2016-04-07 08:31:38
回答 1查看 422关注 0票数 1

我正在尝试从我正在开发的Android应用程序中执行Facebook。我知道我们必须使用图形API。但我不知道要让它发挥作用需要什么。这里有示例代码吗?

我查看了这个链接:Publishing checkins on facebook through android app

我如何使用这段代码?在这方面的任何帮助都将不胜感激。

代码语言:javascript
复制
public class FBCheckin extends Fragment implements View.OnClickListener {

Button checkin;

public String pageID;
String FACEBOOK_LINK = "https://www.facebook.com/pizzaguydelivery";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    super.onCreateView(inflater, container, savedInstanceState);
    final View rootview = inflater.inflate(R.layout.fblogin, container, false);
    checkin.setOnClickListener(this);

    return rootview;
}



@Override
public void onClick(View v) {
    LoginManager.getInstance().logInWithPublishPermissions(getActivity(), Arrays.asList("publish_actions"));
    pageID = FACEBOOK_LINK.substring(FACEBOOK_LINK.lastIndexOf("/") + 1, FACEBOOK_LINK.length());
    new GraphRequest(AccessToken.getCurrentAccessToken(), "/" + pageID, null, HttpMethod.GET, new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
                                /* handle the result */

            try {
                if (response.getError() == null) {
                    JSONObject obj = response.getJSONObject();
                    if (obj.has("id")) {
                        pageID = obj.getString("id");

                        Bundle params = new Bundle();
                        params.putString("message", "Eating");
                        params.putString("place", pageID);
                        // params.putString("link", "URL");

                        if (pageID == null) {
                            //Toast.makeText(getApplicationContext(),"Failed to check in",Toast.LENGTH_SHORT).show();
                        } else {
                            new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/feed", params, HttpMethod.POST, new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {

                                    if (response.getError() == null) {
                                        //success
                                    } else {
                                        //Toast.makeText(getApplicationContext(),"Failed to check in",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }).executeAsync();
                        }
                    }
                }
            } catch (JSONException q) {
                // TODO Auto-generated catch block
                //Toast.makeText(getApplicationContext(),q.getMessage(),Toast.LENGTH_SHORT).show();
                q.printStackTrace();
            }


        }


    }).executeAsync();
}


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-07 08:58:45

下面的代码仅通过pageid登录到带有facebook页面的位置。(比如餐馆)但你认为你也可以用地点身份来做。

代码语言:javascript
复制
    LoginManager.getInstance().logInWithPublishPermissions(
                            getActivity(), Arrays.asList("publish_actions"));
                    String PAGEID = FACEBOOK_LINK.substring(
                            FACEBOOK_LINK.lastIndexOf("/") + 1,
                            FACEBOOK_LINK.length());
                    new GraphRequest(AccessToken.getCurrentAccessToken(), "/"
                            + PAGEID, null, HttpMethod.GET,
                            new GraphRequest.Callback() {
                                public void onCompleted(GraphResponse response) {
                                    /* handle the result */

                                    try {
                                        if (response.getError() == null) {
                                            JSONObject obj = response
                                                    .getJSONObject();
                                            if (obj.has("id")) {
                                                pageID = obj.getString("id");
/* make the API call */
                                    Bundle params = new Bundle();
                                    params.putString("message", "Eating");
                                    params.putString("place", pageID);
                                    params.putString("link","URL");
                                    if (pageID == null) {
                                        MessageBox
                                                .Show(getActivity(),
                                                        "Failed to check in to this restaurant");
                                    } else {
                                        new GraphRequest(AccessToken
                                                .getCurrentAccessToken(),
                                                "/me/feed", params,
                                                HttpMethod.POST,
                                                new GraphRequest.Callback() {
                                                    public void onCompleted(
                                                            GraphResponse response) {

                                                        if (response.getError() == null) {
                                                            //success
                                                        } else {
                                                            MessageBox
                                                                    .Show(getActivity(),
                                                                            "Failed to check in to this restaurant");
                                                        }
                                                    }
                                                }).executeAsync();
                                            }
                                        } 
                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        MessageBox.Show(getActivity(),
                                                "Error: " + e.getMessage());
                                        e.printStackTrace();
                                    }


                                    }

                                }
                            }).executeAsync();

我知道这段代码有点混乱,但它完成了工作。在这里,我做的第一件事是向用户请求facebook发布许可。然后,通过图形请求检查正在签入的pageID是否有效。如果它是有效的,那么我将通过另一个请求将用户签入到他想要的位置。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36470753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档