0

I'm getting error when i try to get the value of Count(id) of my database. I want to get the value and put him in an textView.

This is my java code (the response is already constructed as an array)

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);
    }

This is my php code:

<?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);
?>
Asfo
  • 413
  • 10
  • 20

2 Answers2

0

I suppose there is a problem with name of Json field - COUNT(id). Modify your SQL request:

SELECT COUNT(id) as count FROM divisao;

and than parse the value from JSON as you did:

JSONObject jObj = new JSONObject(finalResult);
String count= jObj.getString("count"); //changed "COUNT(id)" to "count"
tv.setText(count);
0

You don't read the JSONArray response object :

 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();
    }

}

it should be :

 public void onResponse(JSONArray response) {
    try {
             JSONObject jObj = new JSONObject(response.get(0));
             String count= jObj.getString("COUNT(id)");
             tv.setText(count);
    } catch (JSONException e) {
         e.printStackTrace();
    }

}

Pascal Heraud
  • 365
  • 2
  • 8
  • Yes, thats it! just had to put (String.valueOf(response.get(0))); and it worked, thanks you so much! – Fabio Gonçalves Dec 28 '16 at 23:33
  • And if I want to return that value?what should I do? – Fabio Gonçalves Dec 29 '16 at 09:11
  • You can't set it into result because it's final. You can call a method on the activity from inside Response, for instance setCount(int) that will store it or display it. – Pascal Heraud Dec 29 '16 at 10:31
  • Hm okey thanks! Is it possible when I insert a value in database to get the id of json request?For example: I insert id(autoincremente) and description = var, is it possible to receive that id? – Fabio Gonçalves Dec 29 '16 at 22:00
  • Please dont ask others question in this one, the page will become too harsh to read. For your problem, see there I think : http://stackoverflow.com/questions/897356/php-mysql-insert-row-then-get-id – Pascal Heraud Dec 29 '16 at 22:05