开发者

How do I get a Facebook stream and user profile on Android?

开发者 https://www.devze.com 2023-03-10 06:25 出处:网络
In my android application, I want to get stream and users at the same time. Like some of other applications, I want to show news feed with user\'s pic and name.

In my android application, I want to get stream and users at the same time. Like some of other applications, I want to show news feed with user's pic and name. But when I used graph api, it was difficult to get a profile picture.

I switched over to FQL and by using 2 queries I could get streams and user's picture as well. The problem is that if the page posts something, I could read that page but couldn't get page's profile at the same time, so it wouldn't work well.

So my question is if there is any solution to show stream and user's profile picture at the same time? It doesn't matter if I have to use graph api or fql, whatever.

This is query that I can get all stream.

 requestPost("SELECT source_id, target_id, message FROM stream WHERE filter_key in "
                + "(SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0 limit 20");


public String requestPost(String fql) throws MalformedURLException,
IOException, JSONException, FacebookError {

    Bundle parameters = new Bundle();
    parameters.putString("format", "json");
    parameters.putString("query", fql);
    parameters.putString("method", "fql.query");

    if (mFacebook.isSessionValid()) {
        parameters.putString("access_token", mFacebook.getAccessToken());
    }
    String url = (fql != null) ? "https://api.facebook.com/method/fql.query?query=QUERY"
            : "https://api.facebook.com/restserver.php";
    String response = mFacebook.request(parameters);
    response = "{\"data\":" + response + "}";

    JSONObject json = Util.parseJson(response);
    JSONArray data = json.getJSONArray("data");
    // ///////////////fql/////////////////////////////////

    posts.removeAll(posts);
    for (int i = 0; i < data.length(); i++) {
        JSONObject jpost = data.getJSONObject(i);
        Post po = new Post();
        po.po_fromid = jpost.getString("source_id");
        po.po_message = jpost.getString("message");
        requestUser("SELECT name, pic_square FROM user WHERE uid ="
                + po.po_fromid);

        posts.add(po);

    }
    Message msg = new Message();
    msg.what = 0;

    mHandler.sendMessage(msg);

    return Util.openUrl(url, "GET", parameters);

}

public String requestUser(String fql) throws FileNotFoundException,
        MalformedURLException, IOException, JSONException, FacebookError {

    // ///////////////fql/////////////////////////////////
    Bundle parameters = new Bundle();
    parameters.putString("format", "json");
    parameters.putString("query", fql);
    parameters.putString("method", "fql.query");

    if (mFacebook.isSessionValid()) {
        parameters.putString("access_token", mFacebook.getAccessToken());
    }
    String url = (fql != null) ? "https://api.facebook.com/method/fql.query?query=QUERY"
            : "https://api.facebook.com/restserver.php";
    String response = mFacebook.request(parameters);
    response = "{\"data\":" + response + "}";

    JSONObject json = Util.parseJson(response);
    JSONArray data = json.getJSONArray("data");
    // ///////////////fql/////////////////////////////////
    friends.removeAll(friends);
    for (int i = 0; i < data.length(); i++) {
        JSONObject me = data.getJSONObjec开发者_如何学Ct(i);
        Friend fr = new Friend();
        fr.frname = me.getString("name");
        fr.pic = me.getString("pic_square");
        friends.add(fr);
    }
    return Util.openUrl(url, "GET", parameters);

}


You shouldn't need to query separately for the picture, just the stream. The stream results will have the user id, and you can use the user id to build the picture url automatically. The url format is:

https://graph.facebook.com/######/picture?type=??????

Where ##### is their user id, and ?????? is the picture type (square,small,normal,large).

0

精彩评论

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

关注公众号