简体   繁体   English

使用Facebook Android登录

[英]Login using facebook android

i am creating login with facebook. 我正在用Facebook创建登录名。 User logins with facebook just 1 time and i need to get it's facebook userID. 用户使用facebook登录仅1次,我需要获取它的facebook userID。

There is the code i am using: 有我正在使用的代码:

// Login with facebook
    public void loginFB(View view) {

        System.out.println("started");
        facebook = new Facebook(kAppId);

        facebook.authorize(this, new DialogListener() {

            @Override
            public void onComplete(Bundle values) {
                System.out.println("onComplete");

                new Thread(new Runnable() {
                    public void run() {
                        JSONObject me = null;
                        try {
                            me = new JSONObject(facebook.request("me"));
                        } catch (MalformedURLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        try {
                            System.out.println("Login with fb");
//here i should get it's user id                            
String id = me.getString("id");
                            System.out.println("id = " + id);                                                       



                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }).start();

            }


            @Override
            public void onFacebookError(FacebookError error) {
                error.getMessage();
                System.out.println("onFacebookError");
            }

            @Override
            public void onError(DialogError e) {
                e.getMessage();
                System.out.println("DialogError");
            }

            @Override
            public void onCancel() {
                System.out.println("onCancel");
            }

        });

    }

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            facebook.authorizeCallback(requestCode, resultCode, data);  
    }  

So i get an exception on callback to this method: 所以我在回调到此方法时遇到异常:

        @Override
        public void onError(DialogError e) {
            e.getMessage();
            System.out.println("DialogError");
        }

And the exception: 和例外:

com.facebook.android.DialogError: The connection to the server timed out.

So i should change something in the code or there are some other problem ? 所以我应该在代码中更改某些内容,否则会有其他问题?

Basically i need user to login just one time when he downloads an app and i need to get his user id. 基本上,我需要用户在下载应用程序时仅登录一次,并且我需要获取他的用户ID。

Thanks. 谢谢。

EDIT: 编辑:

I had added permission to internet. 我已经添加了对互联网的许可。 So the problem was with my internet. 所以问题出在我的互联网上。 Facebook wasn't reachable. Facebook无法访问。

How ever now the problem is that: in this line: 现在的问题是:在这一行中:

String id = me.getString("id");

me is returning null. 我返回null。

Did you modified you AndroidManifest.xml to allow the app to access the internet? 您是否修改了AndroidManifest.xml以允许该应用访问互联网?

<uses-permission android:name="android.permission.INTERNET"/>

http://developers.facebook.com/docs/mobile/android/build/#xml http://developers.facebook.com/docs/mobile/android/build/#xml

In the JSONObject you need to do something like this: 在JSONObject中,您需要执行以下操作:

JSONObject me =  Util.parseJson((facebook.request("me"));

/me will return a JSON object /me将返回一个JSON对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM