org.json.JSONException:电子邮件没有值
Posted
技术标签:
【中文标题】org.json.JSONException:电子邮件没有值【英文标题】:org.json.JSONException: No value for email 【发布时间】:2013-07-29 09:26:17 【问题描述】:在尝试访问用户详细信息时,我收到此错误:
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"
【问题讨论】:
我在使用 FacebookSDK 4.0 时遇到了类似的问题。你已经解决了吗? 【参考方案1】: 由于您正在尝试访问电子邮件而引发异常 但是用户没有使用邮箱注册,使用手机 注册号码。 您必须在代码中检查是否有儿子 response 有键,然后才尝试提取它的值 键if(object.has("email"))
userEmail = object.getString("email");
【讨论】:
【参考方案2】:您是否授予“电子邮件”权限以请求 Facebook 应用程序访问任何用户的电子邮件 ID?
如果您想使用 API 访问 Facebook 用户的电子邮件 ID,那么您必须授予电子邮件权限。有关用户个人资料的一般信息不会向您提供 Facebook 用户的电子邮件地址。
Email Permissions in Facebook API
注意:有些用户还设置了安全措施,不要将任何电子邮件地址分享给任何其他人,那么您也可能无法获得电子邮件地址。
希望对你有所帮助。
【讨论】:
是的,我正在使用我的帐户进行测试,并检查了 fb 上的权限,它们设置正确。但是我没有将这些细节公开。可能这是个问题? 首先通过记录来检查响应并检查它是否包含任何电子邮件字段? 我已经打印了要记录的详细信息,请参阅代码中的 onCompleted。但是我没有在 logcat 上得到它。【参考方案3】:您确实需要获得许可才能获取某人的电子邮件,尽管有些人设置了他们的隐私,因此您可以在没有此明确许可的情况下获取他们的电子邮件,大多数人不需要。
【讨论】:
我已设置权限。请参阅问题的下部。【参考方案4】:这段代码在我获得所需权限方面运行良好 这也将帮助您获得电子邮件许可
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();
);
);
【讨论】:
【参考方案5】:你有这样的检查:
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);
;
【讨论】:
但这不是问题,我得到的是 jsonexception 而不是 NPE。【参考方案6】:试试这个以获得电子邮件的价值 已编辑:默认情况下您不会收到电子邮件,对于电子邮件,您必须指定权限。
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);
【讨论】:
以上是关于org.json.JSONException:电子邮件没有值的主要内容,如果未能解决你的问题,请参考以下文章
org.json.JSONException: org.json.JSONArray 类型的Value[] 无法转换为 JSONObject
org.json.JSONException:overview_polyLine 没有值
类型 org.json.JSONException 被定义多次
java.lang.classnotfoundexception org.json.jsonexception
在尝试解析 JSON 数据 W/System.err 时出现此错误:org.json.JSONException:对客户没有价值