我认为使用以下代码jsonarray不会被包装。 因为我调试代码但似乎没有进入for循环。 我提供了PHP代码和android代码的一部分。 请帮我。
我在onPostExecute中编写了以下代码
if (success == 1) {
// Getting Array of users
try{
JSONArray users=json.getJSONArray(TAG_USERS);
// looping through All Products
for (int i = 0; i < users.length(); i++) {
Log.d("check", "success");
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
String name = c.getString(TAG_UNAME);
Log.d("name....",name);
usersList.add(name);
}
} catch(JSONException e)
{
e.printStackTrace();
}
}
这是php代码
<?php
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("SELECT *FROM userdetails") or die(mysql_error());
if (mysql_num_rows($result) >0) {
// looping through all results
$response["users"] = array();
While($result = mysql_fetch_array($result))
{
$users = array();
$users["uid"] = $result["uid"];
$users["firstName"] = $result["firstName"];
$users["lastName"] = $result["lastName"];
$users["userName"] = $result["userName"];
array_push($response["users"], $users);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No user found";
// echo no users JSON
echo json_encode($response);
}
?>