【问题标题】:I can not bring out any value from GraphRequest().executeAsync()我无法从 GraphRequest().executeAsync() 中提取任何值
【发布时间】:2018-05-23 09:17:24
【问题描述】:

我正在使用 facebook 的图形 api for android。我无法从 GraphRequest([...]).executeAsync() 中提取任何值。所以返回字符串总是空的

public String getPost(){
        new GraphRequest(
                AccessToken.getCurrentAccessToken(), "me?fields=friends,name", null, HttpMethod.GET,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {

                        JSONObject object = response.getJSONObject();
                        try {
                            friends = object.getJSONObject("friends").getJSONObject("summary").getString("total_count");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        };

                    }
                }
        ).executeAsync();

        return friends;
}

【问题讨论】:

  • 当然,这是一个异步调用。它的字面意思是executeAsyncfriends 在您返回时没有设置,它设置在 onCompleted 中,它将在将来的某个时间调用,特别是当 GraphRequest 完成时(因此名称为 onCompleted)。这就像问为什么你的信在放入邮箱后没有神奇地出现在你的手中。

标签: java android android-studio facebook-graph-api


【解决方案1】:

return friends 将返回默认的friends。你在这里没有得到executeAsync() 的行为。 executeAsync() 将异步运行,方法将在调度 executeAsync() 任务后立即返回。

数据,即friends,正在回调方法内部进行修改,因此您必须从onCompleted() 返回它。

要返回数据,您可以使用回调接口参见How to define callback

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2017-06-08
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    • 2017-09-16
    相关资源
    最近更新 更多