对于android中的某些数据,JSONObject数组返回null
我对Android的JSON有问题。出于某种原因,当我使用JSONObject.getInt方法接收某些数据时,它会返回一些空数据。并不是我接收到的所有数据都返回null,似乎只有返回null的数据类型是整数,我还有其他整数数据,它们接收到的数据很好,没有任何null。它似乎只与3个整数变量一致。我在某个地方读过这篇文章,认为它是一个bug,但我不确定是否是这样。顺便说一下,我用的是安卓截击 有更好的方法吗对于android中的某些数据,JSONObject数组返回null,android,arrays,json,android-volley,getjson,Android,Arrays,Json,Android Volley,Getjson,我对Android的JSON有问题。出于某种原因,当我使用JSONObject.getInt方法接收某些数据时,它会返回一些空数据。并不是我接收到的所有数据都返回null,似乎只有返回null的数据类型是整数,我还有其他整数数据,它们接收到的数据很好,没有任何null。它似乎只与3个整数变量一致。我在某个地方读过这篇文章,认为它是一个bug,但我不确定是否是这样。顺便说一下,我用的是安卓截击 有更好的方法吗 StringRequest strReq = new StringReq
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.....};
PHP
如果您确定响应中的数据不是空的,但在尝试解析数据时,数据将变为空,并且您也确定数据字段名称正确,则可以执行以下操作作为替代解决方案:
if (user.getString("activitylvl") != null && !user.getString("activitylvl").equalsIgnoreCase("")) {
int activitylvl = Integer.parseInt(user.getString("activitylvl"));
}
它将
JSON
字段作为字符串对象获取,然后将其转换为整数对象。我发现问题出在用户响应名称上,这是一个php问题:
在getUserByEmailAndPassword函数中有一个方法
叫
mysqli_获取_数组(mysqli_查询)
这将获取列名,并将其设置在一个数组中,该数组的键名为mysql数据库中的列名,因为我的名称不正确
不正确
$response[“user”][“activitylvl”]=$user[“activitylvl”]
$response[“用户”][“膳食计划”]=$user[“膳食计划”]
$response[“用户”][“训练计划”]=$user[“训练计划”]
正确的
$response[“user”][“activitylvl”]=$user[“activity”]
$response[“用户”][“膳食计划”]=$user[“膳食计划”]
$response[“用户”][“训练计划”]=$user[“训练计划”] 整数(int)不能为null,因为它有一些值或它保持零。发布您的回复结构。编辑您的问题,使用另一个应用程序获取json并将其发布到问题上。
// 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"));
}