【发布时间】:2017-05-13 06:08:07
【问题描述】:
当我尝试获取我的数据库的 Count(id) 值时出现错误。 我想获取值并将他放在 textView 中。
这是我的 java 代码(响应已经构造为一个数组)
public void test3(View rootView){
String result = null;
try
{
tv = (TextView) rootView.findViewById(R.id.textView);
RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());
String url = "http://brunos.000webhostapp.com/teste/listar_divisoes.php";
final String finalResult = result;
JsonArrayRequest jsonRequest = new JsonArrayRequest
(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject jObj = new JSONObject(finalResult);
String count= jObj.getString("COUNT(id)");
tv.setText(count);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(jsonRequest);
}
这是我的 php 代码:
<?php
include 'database.php';
$pDatabase = Database::getInstance();
$sql = "SELECT COUNT(id) FROM divisao;";
$result = $pDatabase->query($sql);
$rows = array();
while($temp = mysqli_fetch_assoc($result)) {
$rows[] = $temp;
}
echo json_encode($rows);
?>
【问题讨论】:
-
“json_encode($rows);”的输出是什么?
-
[{"COUNT(id)":"2"}] 这是输出
标签: java php android mysql json