如何在 Android 上获取 Facebook 流和用户个人资料?
在我的 Android 应用程序中,我想同时获取流和用户。 与其他一些应用程序一样,我想显示带有用户图片和姓名的新闻提要。 但是当我使用graph api时,很难获取个人资料图片。
我切换到 FQL,通过使用 2 个查询,我还可以获得流和用户的图片。 问题是,如果该页面发布了某些内容,我可以阅读该页面,但无法同时获取页面的个人资料,因此它无法正常工作。
所以我的问题是是否有任何解决方案可以同时显示流和用户的个人资料图片?我是否必须使用 graph api 或 fql 都没关系。
这是我可以获得所有流的查询。
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.getJSONObject(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);
}
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.getJSONObject(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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要单独查询图片,只需查询流即可。流结果将带有用户 id,您可以使用用户 id 自动构建图片 url。网址格式为:
https://graph.facebook.com/## ####/图片?类型=??????
其中 ##### 是他们的用户 ID,而 ??????是图片类型(方形、小、正常、大)。
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).