【发布时间】: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