【发布时间】:2019-01-15 10:04:08
【问题描述】:
我有一个名为hotel_review 的表,我有一个hotel_stars 列,我想使用Volley 显示Android 中所有酒店星级的总和。我想显示酒店评级的平均值,为此我需要列的总和。我想我很接近但没有得到价值观。我没有收到任何错误,但我也无法获取值。
我的php代码:
<?php
require "init.php";
header('Content-Type: application/json;charset:UTF-8');
$hotel_id = $_POST['hotel_id'];
$query = mysqli_query($con, "SELECT SUM(hotel_stars) AS 'Total' FROM hotel_review WHERE hotel_id = '".$hotel_id."';");
$row = mysqli_fetch_assoc($query);
$sum = $row['Total'];
if($query)
{
$code = "getvalue";
$data[]['Total'] = $sum;
$result = array("code"=>$code,"stars" => $data);
echo json_encode($result);
}else {
$code = "not_get";
$msg = "Try Again....";
array_push($response, array("code"=>$code,"msg"=>$msg));
//echo("Error description: " . mysqli_error($con));
header('Content-Type: application/json;charset:UTF-8');
echo json_encode($response);
// echo("Error description: " . mysqli_error($con));
}
mysqli_close($con);
?>
我的安卓代码:
public void getHotelStars()
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, getStars,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response", response);
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
if (code.equals("not_get")) {
Toast.makeText(hotel_reviews.this, "error", Toast.LENGTH_SHORT).show();
} else {
String totalStars = jsonObject.getString("stars");
// String totalCount = jsonObject.getString("total_stars");
Toast.makeText(hotel_reviews.this, ""+totalStars, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(hotel_reviews.this, "error", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("hotel_id",HotelId);
//params.put("division",EmployeeDivision);
return params;
}
};MySingleton.getInstance(hotel_reviews.this).addToRequestque(stringRequest);
}
我在 params.put 中获得了正确的 hotel_id,所以在获取 id 时没有错误。
【问题讨论】:
-
服务器的响应是什么?你和 Postman 核对过吗?
-
我一无所获
-
做一件事,在执行查询后请检查它是否成功或有问题。如果查询成功,还检查查询是否返回任何内容。