获取Facebook信息的问题

时间:2015-05-29 06:07:05

标签: android facebook integration

我想在我的代码中找到facebook个人资料信息。此代码在此处起作用Log.e("in try start", "tryyyyyyyyy");,但之后甚至不执行单个日志。

private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
public void loginToFacebook() {
    // mPrefs = getPreferences(SharedPreferences.);
    //  String access_token = mPrefs.getString("access_token", null);
    //long expires = mPrefs.getLong("access_expires", 0);

    // if (access_token != null) {
    //  facebook.setAccessToken(access_token);
    // }

    // if (expires != 0) {
    //  facebook.setAccessExpires(expires);
    // }

    if (!facebook.isSessionValid()) {
        facebook.authorize(getActivity(),
                new String[] { "email", "publish_actions" },
                new DialogListener() {

            @Override
            public void onCancel() {
                // Function to handle cancel event
            }

            @Override
            public void onComplete(Bundle values) {
                // Function to handle complete event
                // Edit Preferences and update facebook acess_token

                Toast.makeText(getActivity(), "hiiiiii", Toast.LENGTH_SHORT).show();
                //mPrefs=getSharedPreferences("data", getActivity().MODE_PRIVATE); 

                SharedPreferences.Editor editor = mPrefs.edit();
                editor.putString("access_token",
                        facebook.getAccessToken());
                editor.putLong("access_expires",
                        facebook.getAccessExpires());
                editor.commit();
                Log.e("getProfileInformation entry", "getProfileInformation");

                getProfileInformation();
            }



            @Override
            public void onFacebookError(FacebookError e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onError(DialogError e) {
                // TODO Auto-generated method stub

            }

        });
    }
}

public void getProfileInformation() {
    Toast.makeText(getActivity(), "byeeeeeee", Toast.LENGTH_SHORT).show();
    Log.e("getProfileInformation start", "getProfileInformation");
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Profile", response);
            String json = response;
            try {
                Log.e("in try start", "tryyyyyyyyy");

                JSONObject profile = new JSONObject(json);
                // getting name of the user
                Log.d("profile", ""+profile);

                fb_name = profile.getString("name");
                // getting email of the user
                fb_email = profile.getString("email");
                Log.d("fb_name", "naem"+fb_name+"emial"+fb_email);
                //fb_login=true;
                //  fb_Image =  getUserPic(fb_email);
                //  LoginFuction();
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("catchhhhhh", ""+e.getMessage());
            }
        }
        public Bitmap getUserPic(String userID) {
            String imageURL;
            Bitmap bitmap = null;
            Log.d("TAG", "Loading Picture");
            imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
            try {
                bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
            } catch (Exception e) {
                Log.d("TAG", "Loading Picture FAILED");
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        public void onIOException(IOException e, Object state) {
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}

此代码不会给我任何名称或emailId。

1 个答案:

答案 0 :(得分:0)

-Hello Abhishek! - 我尝试使用Facebook sdk4.+,我正在获得完整的个人资料信息。

-Firs all将以下代码添加到oncreate

之前的setcontentview方法中
 FacebookSdk.sdkInitialize(getApplicationContext());

- 然后使用以下代码创建Callbackmanager: -

callbackManager = CallbackManager.Factory.create();

- 使用以下代码添加Permissions: -

permission.add("publish_actions");

-Below代码用于Login

LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(final LoginResult loginResult) {
                            // App code
                        GraphRequest request = GraphRequest.newMeRequest(
                act,
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {


                        // Application code
                        if (!TextUtils.isEmpty(object.toString())) {
                            try {
                                JSONObject jresJsonObject = new JSONObject(object.toString());
                                String id = "", name = "", gender = "";
                                if (!(jresJsonObject.isNull("id"))) {
                                    id = jresJsonObject.getString("id");
                                }
                                if (!(jresJsonObject.isNull("gender"))) {
                                    gender = jresJsonObject.getString("gender");
                                    if (gender.equals("male")) {
                                        gender = "0";
                                    } else {
                                        gender = "1";
                                    }
                                }
                                if (!(jresJsonObject.isNull("name"))) {
                                    name = jresJsonObject.getString("name");
                                }


                            } catch (Exception e) {

                            }
                        }
                        Log.e("graphrequest", response.toString());
                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,gender,link");
        request.setParameters(parameters);
        request.executeAndWait();    
                        }

                        @Override
                        public void onCancel() {
                            Log.i("", "Access Token:: " + "loginResult.getAccessToken()");
                        }

                        @Override
                        public void onError(FacebookException exception) {
                            Log.i("", "Access Token:: " + "loginResult.getAccessToken()");
                        }
                    });


            LoginManager.getInstance().logInWithPublishPermissions(this, permission);

- 最后但至少在OnActivitResult

中添加以下代码
 callbackManager.onActivityResult(requestCode, resultCode, data);

注意: - 这是使用最新的Facebook sdk - 如果它没用,或者你仍然遇到问题,请通知我。