我希望以Json格式發送在我的數據庫上創建的查詢結果。查詢返回多條記錄,我希望用Json發送所有thoes記錄。我的查詢包含字段即。 Restaurant_name
,Address
,type of food
。等等 那麼我該怎麼去?這是我不完整的代碼。在Json中發送查詢結果
<?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中檢索這些數據。將其填充到列表中。這裏是我在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;
}
}