1

I have been able to pull the Facebook newsfeed for the logged in user using the "me/home" Graph API Call and am displaying the result in an activity.

Now I have been trying numerous methods which will query the post's like column and check if the current user has liked a post to display the status. Essentially, I am setting a drawable to indicate the user has liked a post.

I cannot really post any code that I have tried so far simply because none of them work. And believe me, I have tried literally numerous methods.

I would appreciate if someone could at least prod me in the right direction.

EDIT: This is my latest attempt at querying the likes column and comparing the result with the current user's ID:

This code is where I am checking for likes count and adding them to an ArrayList. This is also where I am running the query to get the likes on the post.

// 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

        // QUERY THE LIKES COLUMN TO CHECK YOUR LIKE STATUS ON A POST

        Bundle params = new Bundle();
        params.putString(Facebook.TOKEN, Utility.mFacebook.getAccessToken());
        Utility.mAsyncRunner.request(finalThreadID + "/likes&limit=200", params, new LikesListener());

        // TEST ENDS
        } else {
            String countLikes = "0";
            postLikesCountArrayList.add(countLikes);
        }

And this code block is the Listener (a privte class in the same activity) where the results are checked:

private class LikesListener extends BaseRequestListener {
        @Override
        public void onComplete(final String response, final Object state) {

            try {
                JSONObject JOLikes = new JSONObject(response);

                JSONArray JALikes = JOLikes.getJSONArray("data");

                for (int i = 0; i < JALikes.length(); i++) {
                    JSONObject JOTemp = JALikes.getJSONObject(i);

                    if (JOTemp.has("id"))   {
                        String getTempID = JOTemp.getString("id");

                        if (getTempID.equals(initialUserID))    {

                            Runnable run = new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub


                                    ImageView postYourLike = (ImageView) findViewById(R.id.postYourLike);
                                    postYourLike.setBackgroundResource(R.drawable.btn_icon_liked);
                                }
                            };
                            TestNewsFeeds.this.runOnUiThread(run);
                        }
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • 1
    Sorry, you really do need to show us an example of what you've tried, and **what happened when you did**. Right now you could be pasting your code into Calculator for all we know. – Asherah May 24 '12 at 12:18
  • No problem. Adding the latest attempted code. – Siddharth Lele May 24 '12 at 12:20
  • 1
    @Len: I have updated the post with the code from the latest attempt. And I assure you, there were no calculators involved. ;-) – Siddharth Lele May 24 '12 at 12:30

1 Answers1

2

You can use this fql. Replace the post_id

SELECT likes.can_like, likes.user_likes FROM stream WHERE  post_id = "1274834235_3976149403543"

response would look like this, if user_likes is true, he has liked it

 {
  "data": [
    {
      "likes": {
        "can_like": true, 
        "user_likes": false
      }
    }
  ]
}
Venu
  • 7,243
  • 4
  • 39
  • 54
  • This returns whether a user can or cannot like the post right? I just checked two post, one which I have liked and another I have not, and they both return the same result as in the example above. And another post which shows that a friend has changed the profile picture returns "_false_" – Siddharth Lele May 24 '12 at 12:42
  • I have added user_likes property also. try this – Venu May 24 '12 at 12:44
  • This does indeed return the result properly now. Give me 5 minutes to check if this works and get back to you. Although it looks promising and I have a feeling it will work. :-) – Siddharth Lele May 24 '12 at 12:51
  • It worked. Another problem, but unrelated. Thanks a ton fella. :-) – Siddharth Lele May 24 '12 at 14:05