java - 如何在 Android 中检索 Facebook 用户名和通知?

标签 java android facebook

您好,我需要从 Facebook 个人资料中检索用户名和通知,这是我的 Facebook 连接器代码:

import com.FeedPass.SessionEvents.AuthListener;
import com.FeedPass.SessionEvents.LogoutListener;

public class FacebookConnector {
private Facebook facebook = null;
private Context context;
private String[] permissions;
private Handler mHandler;
private Activity activity;
private SessionListener mSessionListener = new SessionListener();
private String _accessToken;

public FacebookConnector(String appId,Activity activity,Context context,String[] permissions){
    this.facebook = new Facebook(appId);

    SessionStore.restore(facebook, context);

    SessionEvents.addAuthListener(mSessionListener);
    SessionEvents.addLogoutListener(mSessionListener);
    this.context=context;
    this.permissions=permissions;
    this.mHandler = new Handler();
    this.activity=activity;
}

public void login() {
    if (!facebook.isSessionValid()) {
        facebook.authorize(this.activity, this.permissions,new LoginDialogListener());
    }
}

public void logout() {
    SessionEvents.onLogoutBegin();
    AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(this.facebook);
    asyncRunner.logout(this.context, new LogoutRequestListener());
}

public void postMessageOnWall(String msg) {
    if (facebook.isSessionValid()) {
        Bundle parameters = new Bundle();
        parameters.putString("message", msg);
        try {
            String response = facebook.request("me/feed", parameters,"POST");
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        login();
    }
}   

public String getNotifications(){
    String result = null;
    if(facebook.isSessionValid()){
        Bundle bundle = new Bundle();
        bundle.putString(Facebook.TOKEN, _accessToken);
        try{
            result = facebook.request("me/notifications", bundle, "GET");
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
    else{
        login();
    }
    return result;
}

private final class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        SessionEvents.onLoginSuccess();
    }

    public void onFacebookError(FacebookError error) {
        SessionEvents.onLoginError(error.getMessage());
    }

    public void onError(DialogError error) {
        SessionEvents.onLoginError(error.getMessage());
    }

    public void onCancel() {
        SessionEvents.onLoginError("Action Canceled");
    }
}

public class LogoutRequestListener extends BaseRequestListener {
    public void onComplete(String response, final Object state) {
        // callback should be run in the original thread, 
        // not the background thread
        mHandler.post(new Runnable() {
            public void run() {
                SessionEvents.onLogoutFinish();
            }
        });
    }
}

private class SessionListener implements AuthListener, LogoutListener {

    public void onAuthSucceed() {
        SessionStore.save(facebook, context);
    }

    public void onAuthFail(String error) {
    }

    public void onLogoutBegin() {           
    }

    public void onLogoutFinish() {
        SessionStore.clear(context);
    }
}

public Facebook getFacebook() {
    return this.facebook;
}
}

如您所见,我有一种方法来检索用户通知,但我不确定它是否有效,有人知道如何以简单的方式做到这一点吗?

最佳答案

您可以使用以下代码来检索 facebook 用户的名称:

public static String getName() {
        String response;
        String name = "";
        try {
            response = ParseFacebookUtils.getFacebook().request("me");
            JSONObject json = Util.parseJson(response);
            name = json.getString("first_name");
        } 
        catch (MalformedURLException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        } 
        catch (FacebookError e) {
            e.printStackTrace();
        } 
        catch (JSONException e) {
            e.printStackTrace();
        }

        return name;
    }

关于java - 如何在 Android 中检索 Facebook 用户名和通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11700991/

相关文章:

java - Android Room 通用 DAO

java - 替换 GWT 中的 DIV

Android 应用内结算。即使在到期日期之后,取消的订阅仍然有效

java - Android 使用 RecyclerView 滚动时列表可以容纳多少个项目

ios - 如何在 iOS 中从 Facebook 获取电子邮件地址?

Java 动画旋转

java - 类间调用方法

javascript - react native : Tried to run my first app

facebook - 如何删除框周围的边框

ios - Facebook 错误在框架模块中包含非模块化 header