如何将 Facebook Graph api 的结果添加到 Java 中的数组列表

我正在使用 Facebook graph api 来找出用户属于哪些页面。当查询返回一个 json 对象时,它有我需要的东西,但是由于某种原因它不想添加到我的数组列表中。正确的值打印在 log.d 中,它似乎出于某种原因跳过了我的 arraylist。有任何想法吗?


查找页面功能


private ArrayList<String> foundPages;

private JSONObject jsonObject; 


public ArrayList<String> findPages()

{

    accessToken = AccessToken.getCurrentAccessToken();

    foundPages = new ArrayList<>();


    GraphRequest request = GraphRequest.newGraphPathRequest(

            accessToken,

            "/me/accounts",

            new GraphRequest.Callback() {

                @Override

                public void onCompleted(GraphResponse response) {


                    try {


                        jsonObject = response.getJSONObject();

                        for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)

                        {

                         page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");

                            Log.d("viewmodel",page);

                            foundPages.add(page);


                        }


                    } catch (JSONException e) {

                        e.printStackTrace();

                    }

                }

            });


    request.executeAsync();



    return foundPages;


慕盖茨4494581
浏览 134回答 2
2回答

哔哔one

有一种解决此问题的常用方法,即定义一个回调方法,在调用填充这些值后将这些值返回给您,就像这样(我的 java 生锈了,请耐心等待.. .)定义一个接口:&nbsp;interface Callback{&nbsp; &nbsp; &nbsp; &nbsp; void apiResponseCallback(ArrayList<Page> result);//whatever your model is, make the array of that type&nbsp; &nbsp; }然后,在您的常规 findPages 方法中,将其更改为:public void findPages(Callback callback) {&nbsp; &nbsp; //&nbsp; &nbsp; //&nbsp;&nbsp; &nbsp; ........&nbsp; &nbsp; &nbsp;for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("viewmodel",page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundPages.add(page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; callback.apiResponseCallback(foundPages);//here we are returning the data when it is done}然后,当你打电话findPagesfindPages(new Callback() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void apiResponseCallback(ArrayList<Page> result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; here, this result parameter that comes through is your api call result to use, so result will be your populated pages to use.&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}为了完整性:public void findPages(Callback callback){&nbsp; &nbsp; accessToken = AccessToken.getCurrentAccessToken();&nbsp; &nbsp; foundPages = new ArrayList<>();&nbsp; &nbsp; GraphRequest request = GraphRequest.newGraphPathRequest(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accessToken,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "/me/accounts",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new GraphRequest.Callback() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCompleted(GraphResponse response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObject = response.getJSONObject();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i < jsonObject.getJSONArray("data").length(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;page = response.getJSONObject().getJSONArray("data").getJSONObject(i).getString("name");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("viewmodel",page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foundPages.add(page);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback.apiResponseCallback(foundPages);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; request.executeAsync();}&nbsp;

牧羊人nacy

是的。这里:request.executeAsync();触发异步请求。但是你的“当前”线程只是继续做:return&nbsp;foundPages;它返回一个空列表。该列表稍后会被填充,但在该方法返回的那一刻,该列表仍然是空的。或者只是被填满。谁知道呢,因为它会在未来某个未知的时间点异步填充。一个解决方案可能是让一些其他变量/字段告诉您数据已到达并推入列表。或者,该方法可以只发出一个同步请求,简单地阻止调用者继续进行,直到数据到达。你看,你不能两全其美:当你不等待结果到达时,你不应该期望它们立即可用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java