2013-07-29 126 views
3

试图访问用户的详细信息,我得到这个错误没有值:org.json.JSONException:电子邮件

07-29 14:49:47.343: W/System.err(2745): org.json.JSONException: No value for email 
07-29 14:49:47.353: W/System.err(2745):  at org.json.JSONObject.get(JSONObject.java:354) 
07-29 14:49:47.374: W/System.err(2745):  at org.json.JSONObject.getString(JSONObject.java:510) 
07-29 14:49:47.374: W/System.err(2745):  at com.example.healthcity.common.FacebookContainer$3$1.onCompleted(FacebookContainer.java:237) 
07-29 14:49:47.409: W/System.err(2745):  at com.facebook.Request$1.onCompleted(Request.java:264) 
07-29 14:49:47.413: W/System.err(2745):  at com.facebook.Request$4.run(Request.java:1240) 
07-29 14:49:47.423: W/System.err(2745):  at android.os.Handler.handleCallback(Handler.java:725) 
07-29 14:49:47.423: W/System.err(2745):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-29 14:49:47.433: W/System.err(2745):  at android.os.Looper.loop(Looper.java:137) 
07-29 14:49:47.444: W/System.err(2745):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-29 14:49:47.444: W/System.err(2745):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-29 14:49:47.463: W/System.err(2745):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-29 14:49:47.463: W/System.err(2745):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-29 14:49:47.485: W/System.err(2745):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-29 14:49:47.493: W/System.err(2745):  at dalvik.system.NativeStart.main(Native Method) 

我不能够捕捉到断点回调方法。如果你经历了这个错误,请帮助。

下面

是我获取用户详细信息代码:

private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
    //RFR//_("onSessionStateChange----------------"); 
    /////////////// super.onSessionStateChange(state, exception); 
    if (state.isOpened()) { 
     readUserInfo(session); 
    } else if (state.isClosed()) { 
     // Session closed 
    } 
} 

private Request.GraphUserCallback graphCallback = new Request.GraphUserCallback() { 

     // callback after Graph API response with user object 
     @Override 
     public void onCompleted(GraphUser user, Response response) { 
      String name = user.getName(); 
      String gender = (String) user.getProperty("gender"); 
      String email = (String) user.getProperty("email"); 
      String birthday = user.getBirthday(); 
      Log.d("FB----->","name: "+ name +" gender: "+ gender +" email: "+ email +" birthday: " +birthday); 

     } 

}; 

    public void readUserInfo(Session session){ 
     // make request to the /me API 
     Request.executeMeRequestAsync(session, graphCallback); 
    } 

及以下,我已经设置权限:

openRequest.setPermissions(Arrays.asList("user_birthday", "email", "user_location")); 

下面是我从图形API得到的回应,当从浏览器中检查:

{ 
    "id": "1000000XXXXX930", 
    "name": "First Last", 
    "first_name": "First", 
    "last_name": "Last", 
    "link": "http://www.facebook.com/<myusername>", 
    "username": "<myusername>", 
    "gender": "male", 
    "locale": "en_US" 
} 
+0

我有这样一个使用FacebookSDK 4.0 asimilar问题。你已经解决了吗? – stackex

回答

0

你给“电子邮件”权限请求Facebook应用程序访问任何用户的电子邮件ID?

如果您想使用api访问Facebook用户的电子邮件ID,那么您必须给予电子邮件许可。有关用户个人资料的一般信息不会为您提供Facebook用户的电子邮件地址。

Email Permissions in Facebook API

注:一些用户也把安全性不共享电子邮件地址,任何其他然后也可能是情况下,你不会得到的电子邮件地址。

希望它能帮助你。

+0

是的,我正在使用我的帐户进行测试,并检查了fb上的许可,它们已正确设置。但是我没有把这些细节公诸于众。可能是这个问题? – Ankit

+0

首先通过记录来检查响应,并检查它是否包含任何电子邮件字段? –

+0

我已经打印了要记录的详细信息,请参阅代码中的onCompleted。但是我不明白它在logcat上。 – Ankit

0

您确实需要获取某人的电子邮件的权限,虽然有些人有他们的隐私设置,以便您可以在未获得此许可的情况下获得他们的电子邮件,但大多数人不会。

+0

我已设置权限。请参阅问题的下半部分。 – Ankit

0

的代码这件作品是在获得所需的权限 工作的罚款与我这也将帮助您获得电子邮件许可

private void FaceBookSignIn() 
    { 
    facebookBtn.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     Session currentSession = Session.getActiveSession(); 
     if (currentSession == null || currentSession.getState().isClosed()) { 
      Session session = new Session.Builder(context).build(); 
      Session.setActiveSession(session); 
      currentSession = session; 
     } 

     if (currentSession.isOpened()) { 
       Session.openActiveSession(SignIn_Rewards.this, false, new Session.StatusCallback() { 

         @Override 
         public void call(final Session session, SessionState state, 
           Exception exception) { 

          if (session.isOpened()) { 

           Request.executeMeRequestAsync(session, 
             new Request.GraphUserCallback() { 

              @Override 
              public void onCompleted(GraphUser user, 
                Response response) { 
               if (user != null) { 

                fromFB=true; 

               UserID=user.getId(); 
               firstName=user.getFirstName(); 
               Lastname=user.getLastName(); 
               Gender=user.getProperty("gender").toString(); 
               Email=user.getProperty("email").toString(); 
               DateofBirth=user.getBirthday(); 
               try { 
                getToken(); 
               } catch (NoSuchAlgorithmException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
               } 

               } 
              } 
             }); 
          } 
         } 
        }); 
       } 
      // Do whatever u want. User has logged in 

     else if (!currentSession.isOpened()) { 
      // Ask for username and password 
      OpenRequest op = new Session.OpenRequest((Activity) context); 

      op.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO); 
      op.setCallback(null); 

      List<String> permissions = new ArrayList<String>(); 
      permissions.add("publish_stream"); 
      permissions.add("user_likes"); 
      permissions.add("email"); 
      permissions.add("user_birthday"); 
      op.setPermissions(permissions); 

      Session session = new Session.Builder(SignIn_Rewards.this).build(); 
      Session.setActiveSession(session); 
      session.openForPublish(op); 
     } 




    } 
    }) ; 

     } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (Session.getActiveSession() != null) 
      Session.getActiveSession().onActivityResult(this, requestCode, 
        resultCode, data); 

     Session currentSession = Session.getActiveSession(); 
     if (currentSession == null || currentSession.getState().isClosed()) { 
      Session session = new Session.Builder(context).build(); 
      Session.setActiveSession(session); 
      currentSession = session; 
     } 

     if (currentSession.isOpened()) { 
      Session.openActiveSession(this, true, new Session.StatusCallback() { 

       @Override 
       public void call(final Session session, SessionState state, 
         Exception exception) { 

        if (session.isOpened()) { 

         Request.executeMeRequestAsync(session, 
           new Request.GraphUserCallback() { 

            @Override 
            public void onCompleted(GraphUser user, 
              Response response) { 
             if (user != null) { 

              fromFB=true; 

             UserID=user.getId(); 
             firstName=user.getFirstName(); 
             Lastname=user.getLastName(); 
             Gender=user.getProperty("gender").toString(); 
             Email=user.getProperty("email").toString(); 
             DateofBirth=user.getBirthday(); 
             try { 
              getToken(); 
             } catch (NoSuchAlgorithmException e) { 
              // TODO Auto-generated catch block 
              e.printStackTrace(); 
             } 

             } 
            } 
           }); 
        } 
       } 
      }); 
     } 
    } 
+0

是否与你合作? –

0

你必须检查是这样的:

private Request.GraphUserCallback graphCallback = new Request.GraphUserCallback() { 

    // callback after Graph API response with user object 
    @Override 
    public void onCompleted(GraphUser user, Response response) { 
     String name = user.getName(); 
     String gender = (String) user.getProperty("gender"); 
     String email = (String) user.getProperty("email"); 
     String birthday = user.getBirthday(); 

    if(email!=null){ 
    //perform your task here. 
    }else{ 
    Log.v("email","is empty"); 
    } 

     Log.d("FB----->","name: "+ name +" gender: "+ gender +" email: "+ email +" birthday: " +birthday); 

    }}; 
+0

但它不是问题,我得到的是一个jsonexception而不是NPE。 – Ankit

0

尝试使用此方法获取电子邮件的价值 编辑:默认情况下,您将不会收到电子邮件,对于需要指定权限的电子邮件。

Session.StatusCallback statusCallback = new Session.StatusCallback() 
     { 

      @Override 
      public void call(Session session, SessionState state, Exception exception) 
      { 
       if(session.isOpened()) 
       { 
        Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 
        { 

         @Override 
         public void onCompleted(GraphUser user, Response response) 
         { 

          if(user != null) 
          { 
           try 
           { 
            System.out.println("Graph Inner Json"+user.getInnerJSONObject()); 
            String email = user.getInnerJSONObject().getString("email"); 
           } 
          } 
         } 
        } 
       } 
      } 

YourActivityName.openActiveSession(this, true, statusCallback); 

这是我们的openActiveSession()我们指定的权限

private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback statusCallback) 
    { 
     OpenRequest openRequest = new OpenRequest(activity); 
     openRequest.setPermissions(Arrays.asList("user_birthday", "email")); 
     openRequest.setCallback(statusCallback); 

     Session session = new Session.Builder(activity).build(); 

     if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) 
     { 
      Session.setActiveSession(session); 
      session.openForRead(openRequest); 

      return session; 
     } 

     return null; 
    } 

而在最后,你应该在onActivityResult方法做,如:

if(Session.getActiveSession() != null) 
     { 
      Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
     }