android 中的某些数据的 JSONObject 数组 returns null
JSONObject Array returns null for some of the data in android
我在 JSON 到 Android 之间遇到问题。出于某种原因,当我使用 JSONObject.getInt 方法接收到一些数据时,它 returns 一些数据为空。并非我收到的所有数据 returns null 似乎只有 returns null 的数据类型是整数,我还有其他整数数据,它们接收得很好,但它永远不会为空。它似乎只与整数变量中的 3 个一致。我读过这个,因为它是某个地方的错误,但我不确定是否是这样。顺便说一下,我正在使用 Android Volley。
有更好的方法吗?
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
...(OTHER Variables that return fine)
int activitylvl = user.getInt("activitylvl");//get null
int meal_plan = user.getInt("meal_plan");//get null
int workout_plan = user.getInt("workout_plan");//get null
Continues.....};
编辑:
这是我的回复结果
{
"error": false,
"uid": "56551efd883b55.31836995",
"user": {
"name": "Test",
"email": "test@gmail.com",
"goal": "1",
"image": "",
"gender": "M",
"birthdate": "8/6/1993",
"height_cm": "182",
"height_ft": "6",
"height_in": "0",
"weight_kg": "74",
"weight_lbs": "165",
"activitylvl": null,
"calories": "2861.43",
"meal_plan": null,
"workout_plan": null,
"adjust_calories_wd": "0",
"adjust_calories_nwd": "0",
"workout_week": "false, false, false, false, false, false, false",
"created_at": "2015-11-24 21:37:49",
"updated_at": "2015-11-24 21:37:49"
}
}
PHP
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// use is found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["goal"] = $user["goal"];
$response["user"]["image"] = $user["image"];
$response["user"]["gender"] = $user["gender"];
$response["user"]["birthdate"] = $user["birthdate"];
$response["user"]["height_cm"] = $user["height_cm"];
$response["user"]["height_ft"] = $user["height_ft"];
$response["user"]["height_in"] = $user["height_in"];
$response["user"]["weight_kg"] = $user["weight_kg"];
$response["user"]["weight_lbs"] = $user["weight_lbs"];
$response["user"]["activitylvl"] = $user["activitylvl"];
$response["user"]["calories"] = $user["calories"];
$response["user"]["meal_plan"] = $user["meal_plan"];
$response["user"]["workout_plan"] = $user["workout_plan"];
$response["user"]["adjust_calories_wd"] = $user["adjust_calories_wd"];
$response["user"]["adjust_calories_nwd"] = $user["adjust_calories_nwd"];
$response["user"]["workout_week"] = $user["workout_week"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
public function getUserByEmailAndPassword($email, $password) {
//DO this for users db as well
$result = mysqli_query($this->conn,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
// check for result
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
$result = mysqli_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password
if ($encrypted_password == $hash) {
return $result;
}
} else {
return false;
}
}
如果您确定您的响应中的数据不为空,但在您尝试解析它时它变为空,并且您还确定数据字段名称是正确的,您可以执行以下操作替代解决方案:
if (user.getString("activitylvl") != null && !user.getString("activitylvl").equalsIgnoreCase("")) {
int activitylvl = Integer.parseInt(user.getString("activitylvl"));
}
它获取 JSON
字段作为字符串对象,然后将其转换为整数对象。
我发现问题出在用户响应名称上,这是一个 php 问题:
inside the function of getUserByEmailAndPassword there is a method
called
mysqli_fetch_array(mysqli_query);
this grabs the column name and sets it in an array with the key name as the column name in mysql database I had the wrong name.
不正确
$response["user"]["activitylvl"] = $user["activitylvl"];
$response["user"]["meal_plan"] = $user["meal_plan"];
$response["user"]["workout_plan"] = $user["workout_plan"];
正确
$response["user"]["activitylvl"] = $user["activity"];
$response["user"]["meal_plan"] = $user["mealplan"];
$response["user"]["workout_plan"] = $user["workoutplan"];
我在 JSON 到 Android 之间遇到问题。出于某种原因,当我使用 JSONObject.getInt 方法接收到一些数据时,它 returns 一些数据为空。并非我收到的所有数据 returns null 似乎只有 returns null 的数据类型是整数,我还有其他整数数据,它们接收得很好,但它永远不会为空。它似乎只与整数变量中的 3 个一致。我读过这个,因为它是某个地方的错误,但我不确定是否是这样。顺便说一下,我正在使用 Android Volley。
有更好的方法吗?
StringRequest strReq = new StringRequest(Request.Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
...(OTHER Variables that return fine)
int activitylvl = user.getInt("activitylvl");//get null
int meal_plan = user.getInt("meal_plan");//get null
int workout_plan = user.getInt("workout_plan");//get null
Continues.....};
编辑: 这是我的回复结果
{
"error": false,
"uid": "56551efd883b55.31836995",
"user": {
"name": "Test",
"email": "test@gmail.com",
"goal": "1",
"image": "",
"gender": "M",
"birthdate": "8/6/1993",
"height_cm": "182",
"height_ft": "6",
"height_in": "0",
"weight_kg": "74",
"weight_lbs": "165",
"activitylvl": null,
"calories": "2861.43",
"meal_plan": null,
"workout_plan": null,
"adjust_calories_wd": "0",
"adjust_calories_nwd": "0",
"workout_week": "false, false, false, false, false, false, false",
"created_at": "2015-11-24 21:37:49",
"updated_at": "2015-11-24 21:37:49"
}
}
PHP
// get the user by email and password
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
// use is found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["goal"] = $user["goal"];
$response["user"]["image"] = $user["image"];
$response["user"]["gender"] = $user["gender"];
$response["user"]["birthdate"] = $user["birthdate"];
$response["user"]["height_cm"] = $user["height_cm"];
$response["user"]["height_ft"] = $user["height_ft"];
$response["user"]["height_in"] = $user["height_in"];
$response["user"]["weight_kg"] = $user["weight_kg"];
$response["user"]["weight_lbs"] = $user["weight_lbs"];
$response["user"]["activitylvl"] = $user["activitylvl"];
$response["user"]["calories"] = $user["calories"];
$response["user"]["meal_plan"] = $user["meal_plan"];
$response["user"]["workout_plan"] = $user["workout_plan"];
$response["user"]["adjust_calories_wd"] = $user["adjust_calories_wd"];
$response["user"]["adjust_calories_nwd"] = $user["adjust_calories_nwd"];
$response["user"]["workout_week"] = $user["workout_week"];
$response["user"]["created_at"] = $user["created_at"];
$response["user"]["updated_at"] = $user["updated_at"];
echo json_encode($response);
} else {
// user is not found with the credentials
$response["error"] = TRUE;
$response["error_msg"] = "Login credentials are wrong. Please try again!";
echo json_encode($response);
}
public function getUserByEmailAndPassword($email, $password) {
//DO this for users db as well
$result = mysqli_query($this->conn,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
// check for result
$no_of_rows = mysqli_num_rows($result);
if ($no_of_rows > 0) {
$result = mysqli_fetch_array($result);
$salt = $result['salt'];
$encrypted_password = $result['encrypted_password'];
$hash = $this->checkhashSSHA($salt, $password);
// check for password
if ($encrypted_password == $hash) {
return $result;
}
} else {
return false;
}
}
如果您确定您的响应中的数据不为空,但在您尝试解析它时它变为空,并且您还确定数据字段名称是正确的,您可以执行以下操作替代解决方案:
if (user.getString("activitylvl") != null && !user.getString("activitylvl").equalsIgnoreCase("")) {
int activitylvl = Integer.parseInt(user.getString("activitylvl"));
}
它获取 JSON
字段作为字符串对象,然后将其转换为整数对象。
我发现问题出在用户响应名称上,这是一个 php 问题:
inside the function of getUserByEmailAndPassword there is a method called
mysqli_fetch_array(mysqli_query);
this grabs the column name and sets it in an array with the key name as the column name in mysql database I had the wrong name.
不正确
$response["user"]["activitylvl"] = $user["activitylvl"];
$response["user"]["meal_plan"] = $user["meal_plan"];
$response["user"]["workout_plan"] = $user["workout_plan"];
正确
$response["user"]["activitylvl"] = $user["activity"];
$response["user"]["meal_plan"] = $user["mealplan"];
$response["user"]["workout_plan"] = $user["workoutplan"];