开发者

Get list of friends' names and IDs fast

In my app, I get the user's friends with the following:

https://graph.facebook.com/me?fields=id,name&access_token=xxxx

The above is actually done by the Facebook Android SDK, so I'm actually creating the request like this:

    Bundle params = new Bundle();
    params.putString("fields", "name,id");
    asyncFacebookRunner.request("me/friends", params, "GET", listener, null);

I parse the JSON response 开发者_开发知识库like this:

            JSONObject data = Util.parseJson(response);
            JSONArray friendsData = data.getJSONArray("data");
            mDbAdapter.deleteAllFriends();

            for (int i = 0; i < friendsData.length(); i++) {
                JSONObject friend = friendsData.getJSONObject(i);
                mDbAdapter.addFriend(friend.getString("name"),
                        friend.getString("id"));
            }

The Util class is part of the Facebook Android SDK.

On my Android phone with wifi on, this takes about 15-20 seconds to send, retrieve, and parse the response (my account has about 370 friends). I've seen a few other apps that do it in about 2 seconds. What's slowing me down here?


The only little reason for a delay I think is you database adapter, i.e. adding items while parsing through the result.

To optimize a bit, you could remove some of the assignments in your loop and add the data to the database through a single request:

JSONObject json = Util.parseJson(response);
JSONArray friendsData = json.getJSONArray("data");
String ids[] , names[] = new String[friendsData.length()];
for(int i = 0; i < friendsData .length(); i++){ 
        ids[i] = friendsData .getJSONObject(i).getString("id");
        names[i] = friendsData .getJSONObject(i).getString("name");
        }

//TODO Add data to your database
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜