我有一个Android应用程序。我想将get请求发送到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端,这是如何获取Categories数组并生成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,但这里有参考资料如何解析JSON并将其值转换为Array?
/* store json string in the_json */
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("data");
注意:确保在Java端,您必须检查json是否成功,该成功必须== 1并且the_json_array不为null
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句