JSON数组响应

时间:2015-12-18 14:08:10

标签: php android json jsonobjectrequest

我无法从mysql数据库获取Array(名称,姓氏和年龄)。我的网络服务正在运行,但我没有在Android应用程序中得到任何响应。

这是我的php文件:

<?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
    include 'connection.php';
    showStudent();
}

function showStudent()
{
    global $connect;

    $query = " Select * FROM demo; ";

    $result = mysqli_query($connect, $query);
    $number_of_rows = mysqli_num_rows($result);

    $temp_array  = array();

    if($number_of_rows > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $temp_array[] = $row;
        }
    }

    header('Content-Type: application/json');
    echo json_encode(array("demo"=>$temp_array));
    mysqli_close($connect);

}

?>

我的php正在生成我测试过的json数组。这是我在Android应用程序中有问题的JSONObjectRequest:

public void onClick(View view) {
                System.out.println("ww");
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                        showUrl, null, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response.toString());
                        try {
                            JSONArray demos = response.getJSONArray("demo");
                            for (int i = 0; i < demos.length(); i++) {
                                JSONObject demo = demos.getJSONObject(i);

                                String firstname = demo.getString("name");
                                String lastname = demo.getString("lastname");
                                String age = demo.getString("age");

                                tvResult.append(firstname + " " + lastname + " " + age + " \n");
                            }
                            tvResult.append("===\n");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.append(error.getMessage());

                    }
                });
                requestQueue.add(jsonObjectRequest);

欢迎任何帮助,我没有找到任何解决方案。我的猜测是,这个可以控制的参数正在搞乱,但如果我删除它,我会得到一大堆错误。

0 个答案:

没有答案