我正在使用Facebook SDK在墙上发布消息.
现在我需要获取Facebook好友列表.任何人都可以帮我吗?
- 编辑 -
try {
Facebook mFacebook = new Facebook(Constants.FB_APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);
Bundle bundle = new Bundle();
bundle.putString("fields", "birthday");
mFacebook.request("me/friends", bundle);
} catch(Exception e){
Log.e(Constants.LOGTAG, " " + CLASSTAG + " Exception = "+e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
当我执行我的时候activity,我没有看到任何东西,但是LogCat有一个调试消息,如:
06-04 17:43:13.863: DEBUG/Facebook-Util(409): GET URL: https://graph.facebook.com/me/friends?format=json&fields=birthday
Run Code Online (Sandbox Code Playgroud)
当我尝试直接从浏览器访问此URL时,我收到以下错误响应:
{
error: {
type: "OAuthException"
message: "An active access token must be used to query information about the current user."
}
}
Run Code Online (Sandbox Code Playgroud)
Kon*_*Kon 12
你差不多到了一半.您已发送请求,但尚未定义任何内容以接收包含结果的响应.您可以扩展BaseRequestListener类并实现其onComplete方法来执行此操作.像这样的东西:
public class FriendListRequestListener extends BaseRequestListener {
public void onComplete(final String response) {
_error = null;
try {
JSONObject json = Util.parseJson(response);
final JSONArray friends = json.getJSONArray("data");
FacebookActivity.this.runOnUiThread(new Runnable() {
public void run() {
// Do stuff here with your friends array,
// which is an array of JSONObjects.
}
});
} catch (JSONException e) {
_error = "JSON Error in response";
} catch (FacebookError e) {
_error = "Facebook Error: " + e.getMessage();
}
if (_error != null)
{
FacebookActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Error occurred: " +
_error, Toast.LENGTH_LONG).show();
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在您的请求中,您可以指定用于接收请求响应的请求侦听器,如下所示:
mFacebook.request("me/friends", bundle, new FriendListRequestListener());
Run Code Online (Sandbox Code Playgroud)
使用FQL查询
String fqlQuery = "SELECT uid, name, pic_square FROM user WHERE uid IN " +
"(SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 25)";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session = Session.getActiveSession();
Request request = new Request(session,"/fql", params,HttpMethod.GET, new Request.Callback(){
public void onCompleted(Response response) {
Log.i(TAG, "Result: " + response.toString());
try{
GraphObject graphObject = response.getGraphObject();
JSONObject jsonObject = graphObject.getInnerJSONObject();
Log.d("data", jsonObject.toString(0));
JSONArray array = jsonObject.getJSONArray("data");
for(int i=0;i<array.length();i++)
{
JSONObject friend = array.getJSONObject(i);
Log.d("uid",friend.getString("uid"));
Log.d("name", friend.getString("name"));
Log.d("pic_square",friend.getString("pic_square"));
}
}catch(JSONException e){
e.printStackTrace();
}
}
});
Request.executeBatchAsync(request);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42009 次 |
| 最近记录: |