我正在开发一个Android应用程序,我需要在Facebook上登录并获取Facebook好友。
我正在使用以下代码获取Facebook好友:
公共类FriendsRequestListener实现RequestListener {
/**
* Called when the request to get friends has been completed.
* Retrieve and parse and display the JSON stream.
*/
public void onComplete(final String response) {
try {
// process the response here: executed in background thread
Log.d("Facebook-Example-Friends Request", "response.length(): " +
response.length());
Log.d("Facebook-Example-Friends Request", "Response: " + response);
final JSONObject json = new JSONObject(response);
JSONArray d = json.getJSONArray("data");
int l = (d != null ? d.length() : 0);
Log.d("Facebook-Example-Friends Request", "d.length(): " + l);
for (int i=0; i<l; i++) {
JSONObject o = d.getJSONObject(i);
String n = o.getString("name");
String id = o.getString("id");
}
// Only the original owner thread can touch its views
} catch (JSONException e) {
Log.w("Facebook-Example", "JSON Error in response");
}
}
@Override
public void onComplete(String response, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onIOException(IOException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFileNotFoundException(FileNotFoundException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onMalformedURLException(MalformedURLException e, Object state) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e, Object state) {
// TODO Auto-generated method stub
}
}
调用此类的方法:
mAsyncRunner = new AsyncFacebookRunner(facebook);
mAsyncRunner.request("me/friends", new FriendsRequestListener());
问题是我的代码在上面的代码调用时没有做任何事情。
答案 0 :(得分:0)
这是一个 v1.0 api,允许用户获取所有好友列表
答案 1 :(得分:-1)
要获取所有Facebook好友列表,您需要获得“taggable_friends”
GraphRequestAsyncTask r = GraphRequest.newGraphPathRequest(fbToken,
"/me/taggable_friends", new Callback() {
@Override
public void onCompleted(GraphResponse response) {
//here you can parse the response
}
}
fbToken
是您登录时的令牌。但是,facebook使用分页,所以你需要从朋友请求获取下一页然后下一个值,这样你就可以解析它。
response.getJSONObject("paging").getString("next");
使用StringRequest获取下一页用户
StringRequest sr = new StringRequest(Request.Method.GET, next,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject friends = null;
try {
friends = new JSONObject(response);
parseResponse(friends);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
检查完整代码Facebook friends list