如何将Facebook详细信息检索到Android应用程序

时间:2015-02-12 06:20:55

标签: android

我将我的Android应用程序集成到Facebook以获取用户的名称到我的应用程序...以下代码我使用....我通过添加断点调试检查...它正在进入getuserdetails()方法但是我没有收到用户详细信息.. 我要做什么请帮助我...我在过去15天从事android工作......帮助我学习

private void onSessionStateChange(Session session, SessionState state,   Exception exception) {

  if (state.isOpened()) {
      Log.i(TAG, "Logged in...");
      getUserDetails(session);
  } else if (state.isClosed()) {
      Log.i(TAG, "Logged out...");
  }
  }


private void getUserDetails(final Session session) {
    Request request = Request.newMeRequest(session,

            new Request.GraphUserCallback() {

                public void onCompleted(GraphUser user, Response response) {
                    if (session == Session.getActiveSession()) {
                        if (user != null) {
                            String id = user.getId();
                            String firstName = user.getFirstName();
                            String lastName = user.getLastName();
                            String userName = user.getName();
                         /*   Toast.makeText(
                                    getActivity(),
                                    "Name : " + name + " ID  : " + id
                                            + " First name : " + firstName
                                            + " Last Name : " + lastName,
                                    Toast.LENGTH_LONG).show();*/



                            ParseObject testObject = new ParseObject("TestObject");
                            testObject.put("name",userName);
                            testObject.saveInBackground();


                        }

                    }

                    if (response.getError() != null) {
                        Log.e("ERROR", response.getError()
                                .getErrorMessage());
                    }
                }
            });
    request.executeAsync();

}

1 个答案:

答案 0 :(得分:0)

试试这个:

//Method invoked when facebook login button is clicked
     public void onClickFacebookLogin() {

         // Session.openActiveSession(this, true, new Session.StatusCallback() {
         openActiveSession(this, true, 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

                     ((ApplicationConstant)Login.this.getApplication()).setfbSession(session);
                     Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                         // callback after Graph API response with user object

                         @Override
                         public void onCompleted(GraphUser user,
                                 Response response) {
                             // TODO Auto-generated method stub




                             if (user != null) {
                                 final GraphUser graphuser=user;

                                 try{   
                                     new AsyncTask<Void, Void, Void>() {            
                                         String fbemail,fname,lname,mediaid,image_url,gender,dob,gen="";
                                         ProgressDialog progressDialog;
                                         @Override
                                         protected void onPreExecute() {
                                             // TODO Auto-generated method stub
                                             super.onPreExecute();

                                             if (progressDialog == null) {
                                                 progressDialog = createProgressDialog(Login.this);
                                                 progressDialog.show();
                                             } else {
                                                 progressDialog.show();
                                             }
                                         }

                                         @Override
                                         protected Void doInBackground(Void... params) {
                                             Object g = graphuser.asMap().get("email");
                                             if(g==null)
                                             {


                                                 fbemail="";
                                                 fname=graphuser.getFirstName();
                                                 dob=graphuser.getBirthday();
                                                 lname=graphuser.getLastName();
                                                 mediaid=graphuser.getId();
                                                 System.out.println("===>accountname inside null"+fbemail);

                                                 return null;

                                             }
                                             else{
                                                 fbemail= graphuser.asMap().get("email").toString();
                                                 gender =graphuser.asMap().get("gender").toString();
                                                 image_url = "http://graph.facebook.com/"+graphuser.getId()+"/picture?type=square";  
                                                 //accountName =graphuser.getProperty("email").toString();
                                                 System.out.println("fb email====>"+fbemail);
                                                 fname=graphuser.getFirstName();
                                                 lname=graphuser.getLastName();
                                                 mediaid=graphuser.getId();
                                                 dob=graphuser.getBirthday();
                                                 System.out.println("Gender facebook"+gender);
                                                 System.out.println("Image"+image_url);
                                                 if(gender.equals("male"))
                                                 {
                                                     gen="M";
                                                 }else{
                                                     gen="F";
                                                 }


                                                 //Toast.makeText(getApplicationContext(), userid, Toast.LENGTH_LONG).show();
                                                 System.out.println("fb email====>"+fbemail);
                                                 //Toast.makeText(getApplicationContext(), accountName, Toast.LENGTH_LONG).show();
                                                 return null;
                                             }
                                         }

                                         @Override
                                         protected void onPostExecute(Void v) {
                                             super.onPreExecute();
                                             progressDialog.dismiss();

                                 }catch(Exception e) {
                                     //findViewById(R.id.progressbar).setVisibility(View.GONE);
                                     Toast.makeText(getApplicationContext(), "Facebook configuration error!", Toast.LENGTH_LONG).show();

                                 }

                             }

                         }
                     });
                 }
             }
         },Arrays.asList("email"));
     }

不要忘记在 onActivityResult()上进行会话:Session.getActiveSession().onActivityResult(Login.this, rc, resc, datac);