【问题标题】:JSONObject Array returns null for some of the data in androidJSONObject Array为android中的一些数据返回null
【发布时间】:2016-02-29 22:41:48
【问题描述】:

我在使用 JSON 到 Android 时遇到问题。出于某种原因,当我使用 JSONObject.getInt 方法接收到一些数据时,它会返回一些空数据。并非我收到的所有数据都返回 null 似乎只有返回 null 的数据类型是 Integers 并且我有其他 Integer 数据并且他们接收它就很好,而不会为 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;
    }

}

【问题讨论】:

  • 整数 (int) 不能为空,它有一些值或为零。发布您的响应结构。
  • 编辑您的问题,使用另一个应用程序来获取 json 并将其发布在问题上。

标签: android arrays json android-volley getjson


【解决方案1】:

如果您确定您的响应中的数据不为空,但在您尝试解析它时它变为空,并且您还确定数据字段名称正确,您可以执行以下操作作为替代解决方案:

if (user.getString("activitylvl") != null && !user.getString("activitylvl").equalsIgnoreCase("")) {
    int activitylvl = Integer.parseInt(user.getString("activitylvl"));
}

它将JSON字段作为字符串对象,然后将其转换为整数对象。

【讨论】:

    【解决方案2】:

    我发现问题出在用户响应名称上,这是一个 php 问题:

    getUserByEmailAndPassword 函数内部有一个方法 叫

    mysqli_fetch_array(mysqli_query);

    这会抓取列名并将其设置在一个数组中,其中键名作为 mysql 数据库中的列名我有错误的名称。

    不正确

    $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"];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-03
      • 2017-10-16
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多