dimanche 28 juin 2015

How can i parse json array?

I am building an Android app and i need to populate a custom listview with some data from my localhost. I am trying to use volley JsonArrayRequest to get that data under the format of a JSONObject array but all i get is org.json.JSONException: End of input at character 0 of. This is my json array request :

final String URL = "http://ift.tt/1TXdybA";
        JsonArrayRequest productsReq = new JsonArrayRequest(Method.POST, URL, new Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                Log.d("productTAG", response.toString());
                for(int i = 0; i < response.length(); i++)
                {
                    try 
                    {   
                        JSONObject productObj = response.getJSONObject(i);
                        String title = productObj.getString("title");
                        String description = productObj.getString("description");
                        String category = productObj.getString("category");
                        String subCategory = productObj.getString("subCategory");
                        String size = productObj.getString("size");
                        String price = productObj.getString("price");
                        String thumbnailUrl = productObj.getString("image_one");

                        Product product = new Product(title, thumbnailUrl, description, category, subCategory, size, price);
                        products.add(product);

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

            }
        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("products_error", error.getMessage().toString());
                error.printStackTrace();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();

                params.put("req", "products");
                params.put("category", category);
                params.put("subCategory", subCategory);

                return params;
            }
        };
        VolleyCore.getInstance(getActivity()).addToRequestQueue(productsReq); 

The method i am using is POST and under the getParams i am adding a tag named req which has the value products. I am checking for this tag in index.php. This is my function that queries the table:

public function getItems($category, $subCategory)
    {
        $query = mysql_query("SELECT * FROM items WHERE category = '$category' AND sub_category = '$subCategory'") or die(mysql_error());


        $objects = array();
        if($query)
        {
            while($objects = mysql_fetch_assoc($query))
            {
                $final[] = $objects;
            }
        }
        return $final;

    }

Here in index.php i am checking for the request:

if(isset($_POST['req']) && $_POST['req'] == 'products')
    {
        $category = $_POST['category'];
        $subCategory = $_POST['subCategory'];


        $obj = $func->getItems($category, $subCategory);

        echo json_encode($obj);

    }

$func is the instantiation of the class where i keep all the methods working with the database(including getItems()). If i use volley StringRequest i do get a response back, but then again why don't i get the response while using JsonArrayRequest ? What am i doing wrong?

Aucun commentaire:

Enregistrer un commentaire