Android - JSONException 没有价值
2022-09-03 03:15:33
我知道有几个问题在这里发布了相同的主题和错误,但没有一个表明与我的问题相同,所以我决定在这里发布我的问题,希望有人能帮助我指出原因。因此,我正在尝试在我的应用程序中实现登录功能,这是代码:
if (tag.equalsIgnoreCase(login_tag)){
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(mContext);
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
logoutUser(mContext);
Toast.makeText(mContext, json.toString(3), Toast.LENGTH_LONG).show();
db.addUser(json_user.getString(KEY_EMAIL), json_user.getString(KEY_NAME), json.getString(KEY_UID), json.getString(KEY_AVA), json_user.getString(KEY_BDAY), json_user.getString(KEY_COUNTRY), json_user.getString(KEY_PREF), json_user.getString(KEY_SPEND));
// Launch Dashboard Screen
Intent dashboard = new Intent(mContext, DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(dashboard);
// Close Login Screen
((Activity) mContext).finish();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
这是我登录时得到的JSON响应:
{
"tag": "login",
"success": 1,
"error": 0,
"uid": "5123",
"user": {
"email": "abcxyz@gmail.com",
"name": "abc",
"avatar": "avatars/img_hzsxda_2013-03-18-11-03-33.jpg",
"bday": "1991-02-01",
"country": "Australia",
"preferences": "none",
"spending": "none"
}
}
所以显然有一个值,但我仍然在我的logcat中得到这个警告:avatar
03-18 12:06:36.972: W/System.err(24574): org.json.JSONException: No value for avatar
由于没有获得任何值头像,我无法完成,因此登录失败。请帮我找到错误以及如何解决它。谢谢。addUser