Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json.JSONException: No value for email

while trying to access user details, I am getting this error:

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)

I am not able to capture breakpoint on callback method. plz help if you have gone through this error.

below is my code for getting user details:

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);
    }

And below is the permission that I've set:

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

Here is the response i get from the graph api, when checked from browser:

{
  "id": "1000000XXXXX930",
  "name": "First Last",
  "first_name": "First",
  "last_name": "Last",
  "link": "http://www.facebook.com/<myusername>",
  "username": "<myusername>",
  "gender": "male",
  "locale": "en_US"
}
like image 559
Ankit Avatar asked Jul 19 '26 01:07

Ankit


1 Answers

  • Exception is thrown because You are trying to access email
  • But user has not used email for registration and has used phone number for the registration.
  • You have to check the possibility in code that whether the son response has the key and only then try to extract the value of that key

if(object.has("email")){
   userEmail = object.getString("email");
}
like image 163
Devrath Avatar answered Jul 20 '26 14:07

Devrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!