我使用以下PHP代码从服务器上的表中获取员工列表:
DB_Functions.php中的函数:
public function getEmployeeList($name) {
$stmt = $this->con->prepare("SELECT employee_name FROM employees WHERE name = ?");
$stmt->bind_param('s', $name);
if ($stmt->execute()) {
$result = $stmt->get_result();
$stmt->close();
if ($result->num_rows < 1) {
return NULL;
} else {
return $result;
}
}
}
这是我的employees.php文件,用于获取商店的员工列表:
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array('error' => FALSE);
if (isset($_POST['name'])) {
$name = $_POST['name'];
$result = $db->getEmployeeList($name);
if (is_null($result) || !$result) {
$response['error'] = TRUE;
$response['error_msg'] = 'No employees have been added to this profile.';
echo json_encode($response);
} else {
$response['error'] = FALSE;
while ($row = $result->fetch_assoc()) {
$response['employee_list'][] = $row;
}
echo json_encode($response);
}
} else {
$response['error'] = TRUE;
$response['error_msg'] = 'You have not logged in to your store\'s account, please log in first.';
echo json_encode($response);
}
?>
最后,这是我的Android代码,用于获取此列表:
private void loadEmployeeList(final String name) {
String tag_string_req = "req_employees";
pDialog.setMessage("Fetching employees...");
showDialog();
StringRequest stringRequest = new StringRequest(Method.POST, AppConfig.URL_EMPLOYEES, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Employee Response: " + response);
hideDialog();
try {
JSONObject jsonObject = new JSONObject(response);
boolean error = jsonObject.getBoolean("error");
if (!error) {
} else {
String errorMsg = jsonObject.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Connection error", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Employee Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("name", name);
return params;
}
};
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
}
所以,当我收到回复并将其打印到日志时,这就是我得到的:
员工响应:{&#34;错误&#34;:false,&#34; employee_list&#34;:[{&#34; employee_name&#34;:&#34; employee1&#34;},{& #34; employee_name&#34;:&#34;和Employee2&#34;},{&#34; employee_name&#34;:&#34; employee3&#34;}]}
我的问题是,如何提取内容并将其放入列表视图?
答案 0 :(得分:3)
首先,您将从php代码获取JSONObject响应。 “employee_list”对象引用一个Array,您可以解析此结果并将其存储在Arraylist中,以便在Listview实现中使用。
List<String> Employees = new ArrayList<String>();
JSONObject result = new JSONObject(response);
JSONArray json = result.getJSONArray("employee_list");
for (int i=0; i<json.length(); i++) {
JSONObject data= json.getJSONObject(i);
String name = data.getString("employee_name");
Employees.add(name);
}
试试这个...
答案 1 :(得分:0)
您应该在编码之前展平数组。
e.g。
变化:
$response['employee_list'][] = $row;
要:
$employees[] = $row;
编辑:
var_dump $ row以确保其有效并且......
if( isset( $row[ 'employee_name' ] ) )
{
$employees[] = $row[ 'employee_name' ];
}
并在while循环之后:
$response[ 'employees' ] = $employees;
这应该允许你将它作为json数组而不是json对象(employee_list键添加的额外级别)接收它。
答案 2 :(得分:0)
试试这个:
$result = '{"error":false,"employee_list":[{"employee_name":"employee1"},{"employee_name":"employee2"},{"employee_name":"employee3"}]}';
$arr = array();
$t = objToArray($result, $arr);
print_r($t);
public function objToArray($obj, &$arr) {
if (!is_object($obj) && !is_array($obj)) {
$arr = $obj;
return $arr;
}
foreach ($obj as $key => $value) {
if (!empty($value)) {
$arr[$key] = array();
$this->objToArray($value, $arr[$key]);
} else {
$arr[$key] = $value;
}
}
return $arr;
}