来自Android App的Facebook Checkin

时间:2016-04-07 08:31:38

标签: android facebook-graph-api

我正在尝试从我正在开发的Android应用中执行Facebook Checkin。我知道我们必须使用图API。但是我不确定是什么才能让它运转起来。有没有示例代码?

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

如何使用此代码?对此有任何帮助表示赞赏。

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


}

1 个答案:

答案 0 :(得分:1)

以下代码仅通过pageid签入带有Facebook页面的位置。 (就像餐馆一样。)但是想想你也可以用地名id来做。

    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是否有效。如果它有效,那么我通过另一个请求将用户检入他想要的位置。