JSONObject的阵中的一些Android的数据返回null [英] JSONObject Array returns null for some of the data in android

查看:188
本文介绍了JSONObject的阵中的一些Android的数据返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到使用JSON问题到Android。出于某种原因,一些数据时,我收到使用JSONObject.getInt方法,它会返回一些数据空的。不是所有的数据我收到返回null好像只返回null是一个整数,我有其他的整型数据和他们收到它只是罚款,没有它曾经被空的数据类型。它似乎是只用整数变量的3相一致。我读过这个,因为它是作为一个错误的地方,但我不知道是否这就是这种情况。顺便说一下我使用的Andr​​oid乱射。

对此有更好的办法?

  StringRequest strReq =新StringRequest(Request.Method.POST,
            AppConfig.URL_LOGIN,新Response.Listener<串GT;(){        @覆盖
        公共无效onResponse(字符串响应){
            Log.d(TAG,登录响应:+ response.toString());
            hideDialog();            尝试{
                JSONObject的jObj =新的JSONObject(响应);
                布尔误差= jObj.getBoolean(错误);                //检查以JSON错误节点
                如果(!错误){
                    //用户成功登录
                    //创建登录会话
                    session.setLogin(真);                    字符串的uid = jObj.getString(UID);                    JSONObject的用户= jObj.getJSONObject(用户);
                    ...(返回精其他变量)
                    INT activitylvl = user.getInt(activitylvl); //获取空
                    INT meal_plan = user.getInt(meal_plan); //获取空
                    INT workout_plan = user.getInt(workout_plan); //获取空
        继续.....};

编辑:
我的继承人结果我的反应

  {
  错误:假​​的,
  UID:56551efd883b55.31836995,
  用户:{
    名:测试,
    电子邮件:test@gmail.com
    目标:1
    图片: ,
    性别:M,
    生日:1993年8月6日,
    height_cm:182,
    height_ft:6,
    height_in:0,
    weight_kg:74
    weight_lbs:165,
    activitylvl:空,
    卡路里:2861.43,
    meal_plan:空,
    workout_plan:空,
    adjust_calories_wd:0,
    adjust_calories_nwd:0,
    workout_week:假的,假的,假的,假的,假的,假的,假的,
    created_at:2015年11月24日21时37分49秒,
    的updated_at:2015年11月24日21时37分49秒
  }
}

PHP

  //获得通过电子邮件和密码的用户
$ USER = $ DB-GT&; getUserByEmailAndPassword($电子邮件,$密码);如果($用户!= FALSE){
    //使用被发现
    $响应[错误] = FALSE;
    $响应[UID] = $用户[UNIQUE_ID];
    $响应[用户] [名称] = $用户[名];
    $响应[用户] [电子邮件] = $用户[电子邮件];
    $响应[用户] [目标] = $用户[目标];
    $响应[用户] [形象] = $用户[形象];
    $响应[用户] [性别] = $用户[性别];
    $响应[用户] [生日] = $用户[生日];
    $响应[用户] [height_cm] = $用户[height_cm];
    $响应[用户] [height_ft] = $用户[height_ft];
    $响应[用户] [height_in] = $用户[height_in];
    $响应[用户] [weight_kg] = $用户[weight_kg];
    $响应[用户] [weight_lbs] = $用户[weight_lbs];
    $响应[用户] [activitylvl] = $用户[activitylvl];
    $响应[用户] [卡路里] = $用户[卡路里];
    $响应[用户] [meal_plan] = $用户[meal_plan];
    $响应[用户] [workout_plan] = $用户[workout_plan];
    $响应[用户] [adjust_calories_wd] = $用户[adjust_calories_wd];
    $响应[用户] [adjust_calories_nwd] = $用户[adjust_calories_nwd];
    $响应[用户] [workout_week] = $用户[workout_week];
    $响应[用户] [created_at] = $用户[created_at];
    $响应[用户] [的updated_at] = $用户[的updated_at];
    回声json_en code($响应);
}其他{
    //用户没有使用凭据找到
    $响应[错误] = TRUE;
    $响应[ERROR_MSG] =​​登录凭据错误,请重试!
    回声json_en code($响应);
}公共职能getUserByEmailAndPassword($电子邮件,$密码){
    //做到这一点,为用户数据库以及
    $结果= mysqli_query($这个 - >康涅狄格州,SELECT * FROM用户WHERE电子邮件= $电子邮件')或死亡(mysqli_connect_errno());    //检查结果
    $ no_of_rows = mysqli_num_rows($结果);    如果($ no_of_rows大于0){
        $结果= mysqli_fetch_array($结果);
        $盐= $结果['盐'];
        $ encrypted_pa​​ssword = $结果['encrypted_pa​​ssword'];
        $哈希= $这个 - > checkhashSSHA($盐,​​$密码);        //检查密码
        如果($ encrypted_pa​​ssword == $哈希){
            返回$结果;
        }    }其他{
        返回false;
    }}


解决方案

我琢磨出来的问题是与用户响应名,这是一个PHP的问题:


  

getUserByEmailAndPassword的功能里面有一个方法
  名为


  
  

mysqli_fetch_array(mysqli_query);


  
  

这抓起列名,并将其与作为mysql数据库列名我错了名字的键名的数组。


错误的。

$回应[用户] [activitylvl] = $用户[activitylvl];

$回应[用户] [meal_plan] = $用户[meal_plan];

$回应[用户] [workout_plan] = $用户[workout_plan];

正确

$回应[用户] [activitylvl] = $用户[活动];

$回应[用户] [meal_plan] = $用户[mealplan];

$回应[用户] [workout_plan] = $用户[workoutplan];

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

}

解决方案

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

这篇关于JSONObject的阵中的一些Android的数据返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆