English 中文(简体)
在解析图形 API 结果时运行 FQL 查询时, 给出数组索引外出例外
原标题:Running an FQL query while parsing a Graph API result gives arrayindexoutofbound exception

在致电“我/家”图表API之后,在解析JSON结果的同时,我正试图用 FQL 进行另一个查询。 FQL 查询问题在我的前一个问题中得到了解决。

我执行的背景是: 我使用 < code> baseAdapter , 从主要活动发送数据, 我使用多个 < code> JSON 发送来自 < code> JSON 的数据解析, 如果我不制作 FQL 查询, 一切都是桃色的。 但是, 当我介绍 FQL 查询时, 查询总是在 < code> adapter 之后运行, 被设置为 < code> ListView 。 这导致数组索引外的例外。

这是我在解析 JSON 结果时使用的代码, 包括额外的 FQL 查询 。 为了保持代码简短, 我将把相关部分包括在内, 因为其余部分工作正常。 但是, 如果需要更多的话, 我也会这样做 。

// GET THE POST S LIKES COUNT
if (json_data.has("likes")) {
    JSONObject feedLikes = json_data.optJSONObject("likes");
    String countLikes = feedLikes.getString("count");
    postLikesCountArrayList.add(countLikes);

    // TEST STARTS
    Runnable run = new Runnable() {
    @Override
    public void run() {
        graph_or_fql = "fql";
        String query = "SELECT likes.user_likes FROM stream WHERE post_id =  " 
        + finalThreadID + " ";
        Bundle params = new Bundle();
        params.putString("method", "fql.query");
        params.putString("query", query);
        Utility.mAsyncRunner.request(null, params, new LikesListener());
        }
    };
    TestNewsFeeds.this.runOnUiThread(run);                      
    // TEST ENDS
} else {
    String countLikes = "0";
    postLikesCountArrayList.add(countLikes);
}

这是Lexistener 类的代码。 它是一个在同一活动中被宣布为私人类的代码 :

private class LikesListener extends BaseRequestListener {
        @Override
        public void onComplete(final String response, final Object state) {
//          Log.e("response", response);

            try {

                JSONArray JALikes = new JSONArray(response);
//              Log.v("JALikes", JALikes.toString());

                for (int j = 0; j < JALikes.length(); j++) {
                    JSONObject JOTemp = JALikes.getJSONObject(j);
//                  Log.e("JOTemp", JOTemp.toString());

                    if (JOTemp.has("likes"))    {

                        JSONObject optJson = JOTemp.optJSONObject("likes");
//                      Log.v("optJson", optJson.toString());

                        if (optJson.has("user_likes"))  {
                            String getUserLikeStatus = optJson.getString("user_likes");
                            Log.e("getUserLikeStatus", getUserLikeStatus);
                            arrayLikeStatus.add(getUserLikeStatus);
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

我用调试方法发现,坠机的原因是在第二个查询完成之前调用设置的Adapter。 我看到在坠机发生后日志被添加到日志中 。

任何帮助解决问题的帮助都将受到赞赏

UPDATE :在我即将放弃时几乎就找出了解决办法。

最佳回答

<强 > 溶解

因此,必须作出这一修改,而不是像问题中使用的那样,将 " 基地请求倾听者 " 称为 " 基地请求倾听者 " 。

try {
    graph_or_fql = "fql";
    String query = "SELECT likes.user_likes FROM stream WHERE post_id =  "
            + finalThreadID + " ";
//                          Log.d("finalThreadID", finalThreadID);
    Bundle params = new Bundle();
    params.putString("method", "fql.query");
    params.putString("query", query);
//                          Utility.mAsyncRunner.request(null, params, new LikesListener());

    String fqlResponse = Utility.mFacebook.request(params);
//                          Log.e("fqlResponse", fqlResponse);

    JSONArray JALikes = new JSONArray(fqlResponse);
//                          Log.v("JALikes", JALikes.toString());

    for (int j = 0; j < JALikes.length(); j++) {
        JSONObject JOTemp = JALikes.getJSONObject(j);
//                              Log.e("JOTemp", JOTemp.toString());

        if (JOTemp.has("likes"))    {
            JSONObject optJson = JOTemp.optJSONObject("likes");
//                                  Log.v("optJson", optJson.toString());

            if (optJson.has("user_likes"))  {
                String getUserLikeStatus = optJson.getString("user_likes");
//                                      Log.e("getUserLikeStatus", getUserLikeStatus);
                arrayLikeStatus.add(getUserLikeStatus);
//                                      Log.d("arrayLikeStatus", arrayLikeStatus.toString());
            }
        }
    }

} catch (Exception e) {
    // TODO: handle exception
}

希望这样能省点时间 如果他们像我一样被卡住的话

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签