dragon7088 2015-12-14 18:58
浏览 59

在Android应用程序中无法通过php和java从db获取字符串

I have mysql db and php script to get data from db. I cant get array of strings in my android app, textview is empty no matter what I do. I checked my php and it is actually working so i guess i have some error in java code in android:

        bShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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 students = response.getJSONArray("demo");
                            for (int i = 0; i < students.length(); i++) {
                                JSONObject student = students.getJSONObject(i);

                                String name = student.getString("name");
                                String lastname = student.getString("lastname");
                                String age = student.getString("age");

                                tvResult.append(name + " " + lastname + " " + age + " 
");
                            }
                            tvResult.setText("===
");

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

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

                    }
                });
                requestQueue.add(jsonObjectRequest);
            }
        });
    }
}

and my php file:

    <?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("students"=>$temp_array));
        mysqli_close($connect);

    }

?>

I tried php an mysql with postman and my guess is that im missing the obvious in java.

Any help is welcomed.

  • 写回答

1条回答 默认 最新

  • duansaoguan7955 2015-12-14 19:59
    关注

    Query:

    $query = " Select * FROM demo ";
    

    your php is generating an array called: students

     echo json_encode(array("students"=>$temp_array));
    

    and you are trying to get an array called demo

       JSONArray students = response.getJSONArray("demo");
    

    change to:

       JSONArray students = response.getJSONArray("students");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应