在android中从Facebook检索用户名和性别

在android中从Facebook检索用户名和性别,第1张

概述这是我如何检索存储在共享首选项中的信息,然后比较用户名和密码是否存在.如果用户已登录,我可以显示其他活动. SharedPreferences settings = getSharedPreferences(“logindetails”,0); String username = settings.getString("username", ""); String password = 这是我如何检索存储在共享首选项中的信息,然后比较用户名和密码是否存在.如果用户已登录,我可以显示其他活动.

SharedPreferences settings = getSharedPreferences(“logindetails”,0);

String username = settings.getString("username","");    String password = settings.getString("password","");

但现在我正在尝试检索用户的用户名和性别并显示在我的活动中.

我试图解决这个问题,但尚未找到令人信服的结果.

任何人都可以帮助我摆脱我的要求吗?

注意:我也看到了Facebook开发者页面的文档

这是我用来检索用户数据并在另一个活动中显示的代码.面临的问题是在Facebook的响应被削弱之前加载布局.我怀疑这是因为使用了mAsyncRunner,因为它自己进行控制.我想要找到的是在加载布局之前解析它.

public class FaceBookRetrIEval extends Activity{this.facebookConnector = new FacebookConnect(APP_ID,this,getApplicationContext(),PERMS);}public class FacebookConnect {    public FacebookConnect fb = null;    private Facebook facebook = null;    private Context context;    private String[] permissions;    public static final String TAG = "FACEBOOK";    private AsyncFacebookRunner mAsyncRunner;    private SharedPreferences sharedPrefs;    private Progressbar pb;    public  String fbname,fbGender;    private Activity activity;    public String check = "false";    private SessionListener mSessionListener = new SessionListener();;    public FacebookConnect(String appID,Activity activity,Context context,String[] permissions) {        this.facebook = new Facebook(appID);        mAsyncRunner = new AsyncFacebookRunner(facebook);        SessionStore.restore(facebook,context);        SessionEvents.addAuthListener(mSessionListener);        SessionEvents.addlogoutListener(mSessionListener);        this.context = context;        this.permissions = permissions;        this.activity = activity;    }    public voID login() {        if (!facebook.isSessionValID()) {            facebook.authorize(this.activity,this.permissions,new LoginDialogListener());        }    }    public voID getID() {            login();        return;    }    public boolean isSession() {        sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);        String access_token = sharedPrefs.getString("access_token","x");        Long expires = sharedPrefs.getLong("access_expires",-1);        Log.d(TAG,access_token);        facebook.setAccesstoken(null);        facebook.setAccessExpires(-1);        if (access_token != null && expires != -1) {//          facebook.setAccesstoken(access_token);//          facebook.setAccessExpires(expires);        }        return facebook.isSessionValID();    }    // request the facebook to provIDe the response and then parse the response to     // obtain username and gender    private class IDRequestListener implements RequestListener {        @OverrIDe        public voID onComplete(String response,Object state) {            try {                Log.d(TAG,"Response: " + response.toString());                JsONObject Json = Util.parseJson(response);                fbGender = Json.getString("gender");                fbname = Json.getString("name");                check = "true";            } catch (JsONException e) {                Log.d(TAG,"JsONException: " + e.getMessage());            } catch (FacebookError e) {                Log.d(TAG,"FacebookError: " + e.getMessage());            }            Toast.makeText(context,"hello",Toast.LENGTH_SHORT).show();            FacebookConnect.this.runOnUiThread(new Runnable() {                public voID run() {                    pb.setVisibility(Progressbar.VISIBLE);                }            });            return ;        }        @OverrIDe        public voID onIOException(IOException e,Object state) {            Log.d(TAG,"IOException: " + e.getMessage());        }        @OverrIDe        public voID onFacebookError(FacebookError e,"FacebookError: " + e.getMessage());        }        @OverrIDe        public voID onfileNotFoundException(fileNotFoundException e,Object state) {        }        @OverrIDe        public voID onMalformedURLException(MalformedURLException e,Object state) {        }    }    protected voID onActivityResult(int requestCode,int resultCode,Intent data) {        facebook.authorizeCallback(requestCode,resultCode,data);    }    public voID runOnUiThread(Runnable runnable) {    }    private class LoginDialogListener implements DialogListener {        @OverrIDe        public voID onComplete(Bundle values) {            String token = facebook.getAccesstoken();            long token_expires = facebook.getAccessExpires();            Log.d(TAG,"Accesstoken: " + token);            Log.d(TAG,"AccessExpires: " + token_expires);            sharedPrefs = PreferenceManager                    .getDefaultSharedPreferences(context);            sharedPrefs.edit().putLong("access_expires",token_expires).clear()                    .commit();            sharedPrefs.edit().putString("access_token",token).clear().commit();            mAsyncRunner.request("me",new IDRequestListener());            Toast.makeText(context,"You are logged in",Toast.LENGTH_SHORT).show();            return;        }        @OverrIDe        public voID onCancel() {            Log.d(TAG,"OnCancel");        }        @OverrIDe        public voID onFacebookError(FacebookError e) {            // Todo auto-generated method stub        }        @OverrIDe        public voID onError(DialogError e) {            // Todo auto-generated method stub        }    }    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;    }}

有人可以帮帮我吗?

解决方法 最后我能够自己解决我的问题.

private static final String[] PERMS = new String[] { "publish_stream" };----->this.facebookConnector = new FacebookConnect(APP_ID,PERMS);---->JsONObject Json = Util.parseJson(response);fbGender = Json.getString("gender");fbname = Json.getString("name");

这是我的代码如何提取数据

总结

以上是内存溢出为你收集整理的在android中从Facebook检索用户名和性别全部内容,希望文章能够帮你解决在android中从Facebook检索用户名和性别所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1129623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存