简体   繁体   English

将Facebook SDK集成到我的Android应用程序中

[英]Integrate Facebook sdk in my android application

I know there are many tutorials available for this, but doesn't succeeded.. i need to get profile details of logged in user. 我知道有很多可用的教程,但是没有成功。.我需要获取登录用户的个人资料详细信息。

I integrated FAcebook SDK as below.. 我集成了如下的FAcebook SDK。

Session.OpenRequest openRequest = new Session.OpenRequest(Login.this);
    openRequest.setPermissions(Arrays.asList("email", "user_birthday"));
    openRequest.setCallback(new Session.StatusCallback() {
        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {
                // make request to the /me API
                Request request = Request.newMeRequest(session,
                        new Request.GraphUserCallback() {

                            // callback after Graph API response with user
                            // object
                            @Override
                            public void onCompleted(GraphUser gUser,
                                    Response response) {
                                if (gUser != null) {
                                    String email = gUser.getProperty("email").toString();
                                    String firstName = gUser.getFirstName();
                                    String lastName = gUser.getLastName();
                                    String username = gUser.getUsername();
                                    String birthday = gUser.getBirthday();
                                }
                            }

                        });
                request.executeAsync();

            }
        }
    }
    );

Try this with Facebook SDK 4.4.0 Create App ID first then add this to your code. 尝试使用Facebook SDK 4.4.0创建应用ID,然后将其添加到您的代码中。 Initialize the SDK as below.. 如下初始化SDK。

FacebookSdk.sdkInitialize(MainActivity.this);
        setContentView(R.layout.activity_main);

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions(Arrays
                .asList("public_profile, email, user_birthday, user_friends"));

        callbackManager = CallbackManager.Factory.create();
        loginButton.registerCallback(callbackManager,
                new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {
                        new fblogin().execute(loginResult.getAccessToken());
                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onError(FacebookException e) {

                    }
                });

The Asynctask to get profile details.. 用于获取配置文件详细信息的Asynctask。

public class fblogin extends AsyncTask<AccessToken, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(AccessToken... params) {
            GraphRequest request = GraphRequest.newMeRequest(params[0],
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object,
                                GraphResponse response) {
                            Log.v("MainActivity", response.toString());
                            try {
                                String profile_pic = object.getString("id");
                                try {
                                    myurl = new URL(
                                            "https://graph.facebook.com/"
                                                    + profile_pic + "/picture");
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                profilepic = myurl.toString();


                                Log.v("Name", object.getString("first_name"));
                                Log.v("Email", object.getString("email"));
                                Log.v("Profile Pic Url", profilepic);
                                Log.v("Gender", object.getString("gender"));

                            } catch (JSONException jse) {
                                // session.logoutUser();
                                Log.e("fb json exception", jse.toString());
                            }
                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,first_name,email,gender");
            request.setParameters(parameters);
            GraphRequest.executeBatchAndWait(request);
            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
            Intent i = new Intent(MainActivity.this, SecondActivity.class);

            startActivity(i);
            MainActivity.this.finish();
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    }

Add this to AndroidManifest.xml 将此添加到AndroidManifest.xml

 <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id" />

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

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