0

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 + " \n");
                            }
                            tvResult.setText("===\n");

                        } 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
  • you're looking for a demo key in your java code, but your php code generates students... Commented Dec 14, 2015 at 19:02

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.