我希望以Json的格式发送在我的数据库上创建的查询结果。查询返回多个记录,我希望用Json发送所有的记录。我的查询包含字段即。 Restaurant_name
,Address
,type of food
。等等
那我该怎么办呢?这是我不完整的代码。
<?php
require_once 'DB_connect.php';
class populatelist{
private $con;
private $conn;
function __construct()
{
$this->con = new DB_connect();
$this->conn = $this->con->connectWithRestaurant();
}
function selectallfields(){
$query = "SELECT * FROM `restaurant_time` LIMIT 50";
$query_exec = $this->conn->query($query);
if($query_exec->num_rows >0){
$queryresult = $query_exec->fetch_assoc();
}
}
}
?>
我这样做的主要目的是在AndroidListView中检索这些数据。在List中填充它。这是我在android中的代码片段。
class Jsonfetch extends AsyncTask<String,Void,Void>{
@Override
protected Void doInBackground(String... params) {
try {
URL url = new URL("http://172.16.16.88/orderspot/populatelist.php");
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setAllowUserInteraction(false);
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String temp;
StringBuilder stringBuilder = new StringBuilder();
while((temp = bufferedReader.readLine())!=null){
stringBuilder.append(temp);
}
String JsonResponse = stringBuilder.toString();
try {
JSONArray new_array =new JSONArray(JsonResponse);
//int count;
for(int i = 0, count = new_array.length() ; i < count ; i++){
JSONObject jsonObject = new_array.getJSONObject(i);
Resname.add(jsonObject.getString("restname"));
Restaddress.add(jsonObject.getString("restadd"));
Resttime.add(jsonObject.getString("restime"));
images.add(jsonObject.getInt("images"));
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:1)
最好像这样使用发送查询结果
$query['query'] = $yourQueryResult;
echo json_encode($query);
答案 1 :(得分:0)