Search code examples
androidarraysjsonandroid-volleygetjson

JSONObject Array returns null for some of the data in android


I'm getting problems with JSON to Android. For some reason some of the data when i receive it using JSONObject.getInt method it returns some of the data null. Not all the data i receive returns null it seems like only the Data type that returns null is Integers and i have other Integer data and they receive it just fine without it ever being null. It seems to be consistent with only 3 of the integer variables. I've read this as it being being a bug somewhere but I don't know for sure if thats the case. By the way i'm using Android Volley.

Is there a better way for this?

        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.....};

EDITS: Heres my result to my response

{
  "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;
    }

}

Solution

  • I figured it out the problem was with the user response name it was a php problem:

    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.

    INCORRECT

    $response["user"]["activitylvl"] = $user["activitylvl"];

    $response["user"]["meal_plan"] = $user["meal_plan"];

    $response["user"]["workout_plan"] = $user["workout_plan"];

    CORRECT

    $response["user"]["activitylvl"] = $user["activity"];

    $response["user"]["meal_plan"] = $user["mealplan"];

    $response["user"]["workout_plan"] = $user["workoutplan"];