我有一个安卓应用。 我想向 get_categories.php 文件发送获取请求。 在我想要的 get_categories.php 文件中
$query_categories = "SELECT category_name FROM categories";
并将在该表中找到的所有类别返回到 json 数组中。
我该怎么做?
这是我的不完整代码:
if (!empty($_GET)) {
$query_categories = "SELECT category_name FROM categories";
$success = false;
try{
$sth = $connection->prepare($query_categories);
//$sth->execute(array(':user_id' => $user_id));
//$user_items_count = $sth->rowCount(); - these are lines from other php file I've used
foreach($)//??
$success = true;
} catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = $ex;
die(json_encode($response));
$connection = null;
}
if($success) {
$response["success"] = 1;
$response["message"] = "Kylie";
die(json_encode($response));
$connection = null;
} else {
$response["success"] = 2;
$response["message"] = "something went wrong";
die(json_encode($response));
$connection = null;
}
} else {
$response["success"] = 3;
$response["message"] = "Another brick in the wall";
echo json_encode($response);
$connection = null;
}
稍后,在我的 Java 代码中,我如何对其进行解码? 通常到目前为止,在我的其他 JSON 传输中,我收到了没有数组的普通 Json 对象并以这种方式读取它们:
setUserLastSeen(json.getString(TAG_USER_LAST_SEEN)); //for example.
但是我如何解码一个数组呢?
最佳答案
对于 php 端,这是如何获取类别数组并生成 json,对于准备好的语句,这是如何完成的
$success = false;
try {
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$success = true;
}
catch(PDOException $e) {
$response["success"] = 0;
$response["message"] = 'Connection failed: ' . $e->getMessage();
$response["data"]=null;
die(json_encode($response));
}
$query_categories = "SELECT category_name FROM categories";
try{
$sth = $sth->prepare($query_categories );
$success = true;
} catch(PDOException $e) {
$response["success"] = 0;
$response["message"] = 'Prepare failed: ' . $e->getMessage();
$response["data"]=null;
die(json_encode($response));
}
try{
$sth->execute();
$success = true;
} catch(PDOException $e) {
$response["success"] = 0;
$response["message"] = 'Execute failed: ' . $e->getMessage();
$response["data"]=null;
die(json_encode($response));
}
$categories = $sth->fetchAll(PDO::FETCH_COLUMN, 0);/* fetches all categories from db */
/* Output of $query_categories
Array
(
[0] => cat1
[1] => cat2
[2] => cat3
[3] => cat4
)
*/
/* json encode $query_categories
["cat1","cat2","cat3","cat4"]
*/
/* check if categories exist or not*/
if(empty($categories)){
$response["success"] = 0;
$response["message"] = "No categories found";
$response["data"]=null;
die(json_encode($response));
$connection = null;
}
if($success) {
$response["success"] = 1;
$response["message"] = "Kylie";
$response["data"]=$categories;
die(json_encode($response));
$connection = null;
/* output
{"success":0,"message":"Kylie","data":["cat1","cat2","cat3","cat4"]}
*/
} else {
$response["success"] = 2;/* don't where you are setting success to 2*/
$response["message"] = "something went wrong";
$response["data"]=null;
die(json_encode($response));
$connection = null;
}
} else {
$response["success"] = 3;/* don't where you are setting success to 3*/
$response["message"] = "Another brick in the wall";
$response["data"]=null;
die(json_encode($response));
$connection = null;
}
我不擅长 java 但这里有引用 How to parse a JSON and turn its values into an Array?
/* store json string in the_json */
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("data");
Note: make sure on java side you must check the success from json which must be ==1 and the_json_array is not null
关于php - 如何通过php从mysql数据库中获取所有产品类别并返回json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241414/