在Facebook SDK 4(Android)中创建请求

fra*_*ong 3 android facebook facebook-graph-api

据我所知,Facebook SDK v4不再使用Session了,但我在他们的Graph API文档中读到,会话仍然用于创建请求

https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3

/* make the API call */
new Request(
    session,
    "/me",
    null,
    HttpMethod.GET,
    new Request.Callback() {
        public void onCompleted(Response response) {
            /* handle the result */
        }
    }
).executeAsync();
Run Code Online (Sandbox Code Playgroud)

这让我感到困惑,我怎样才能获得Request的会话?使用Facebook Graph API获取用户个人资料数据的最新示例?

任何建议表示赞赏.

小智 14

在Facebook开发者的页面上发布了一个更改日志和升级帖子.在您的情况下,这将是一个选项:

GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject user, GraphResponse response) {
                /* handle the result */
            }
    }).executeAsync();
Run Code Online (Sandbox Code Playgroud)

顺便说一句,他们指定:

默认情况下,newMeRequest方法从用户对象中提取默认字段.如果您需要任何其他字段,或者出于性能原因而希望减少响应有效负载,则可以添加字段参数并请求特定字段.

例如:

GraphRequest request = GraphRequest.newMeRequest(
    accessToken,
    new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(
               JSONObject object,
               GraphResponse response) {
            // Application code
        }
    });
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
Run Code Online (Sandbox Code Playgroud)

我希望我的回答可以帮助你,成为我在stackoverflow中的第一个答案.问候.

参考:Facebook Android SDK升级