org.json.JSONException:没有电子邮件的价值

时间:2013-07-29 09:26:18

标签: android facebook-sdk-3.0

在尝试访问用户详细信息时,我收到此错误:

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"
}

6 个答案:

答案 0 :(得分:0)

您是否已授予“电子邮件”权限,要求Facebook应用程序访问任何用户的电子邮件ID?

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

Email Permissions in Facebook API

注意:有些用户还将安全性设置为不与其他任何人共享电子邮件地址,那么您也可能无法获得电子邮件地址。

希望它会对你有所帮助。

答案 1 :(得分:0)

你确实需要获取某人的电子邮件的权限,尽管有些人设置了他们的隐私,这样你就可以在没有这种明确许可的情况下收到他们的电子邮件,大多数人都没有。

答案 2 :(得分: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();
                                        }

                                      }
                                  }
                              });
                  }
              }
          });
      }
  }

答案 3 :(得分: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);

  }};

答案 4 :(得分: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);
        }

答案 5 :(得分:0)

  • 由于您正在尝试访问电子邮件而引发异常
  • 但是用户未使用电子邮件进行注册,而是使用电话 注册编号。
  • 您必须检查代码中是否有儿子的可能性 响应具有密钥,然后才尝试提取该密钥的值 键

if(object.has("email")){
   userEmail = object.getString("email");
}