1

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://10.0.3.2/androidapi/index.php";
        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?
EDIT
Example of response

06-28 15:05:58.750: D/productTAG(3853): 


[  
   {  
      "_id":"2",
      "user_email":"unemail",
      "title":"fsdfsd",
      "description":"dffdhfg",
      "pri‌​ce":"12",
      "quantity":"12",
      "size":"L",
      "category":"Men",
      "sub_category":"Shoes",
      "imag‌​e_one":"base 64 code…",
      "image_two":"base 64 code..",
      "image_three":"base 64 code"
   },
   {  
      "_id":"3",
      "user_email":"unemail",
      "title":"fsdfsd",
      "description":"dffdhfg‌​",
      "price":"12",
      "quantity":"12",
      "size":"L",
      "category":"Men",
      "sub_category":"Shoes"      ‌​,
      "image_one":"base 64 code",
      "image_two":"base 64 code",
      "image_three":"base 64 code "
   }
]


Using the StringRequest.

7
  • stackoverflow.com/questions/8740381/… Commented Jun 28, 2015 at 14:24
  • I don't have a file with the JSON that i want to populate the listview. Is that the only way to go? Commented Jun 28, 2015 at 14:35
  • How does the Json returned by the response look like? Commented Jun 28, 2015 at 14:52
  • 06-28 15:05:58.750: D/productTAG(3853): [{"_id":"2","user_email":"unemail","title":"fsdfsd","description":"dffdhfg","price":"12","quantity":"12","size":"L","category":"Men","sub_category":"Shoes","image_one":"base 64 code…","image_two":"base 64 code..","image_three":"base 64 code"},{"_id":"3","user_email":"unemail","title":"fsdfsd","description":"dffdhfg","price":"12","quantity":"12","size":"L","category":"Men","sub_category":"Shoes","image_one":"base 64 code","image_two":"base 64 code","image_three":"base 64 code "}] Commented Jun 28, 2015 at 15:13
  • @ffs You should edit your question with the Json rather than have it in a comment. Commented Jun 28, 2015 at 15:55

2 Answers 2

1

You should try this

@Override
public void onResponse(String response) 
    try {
        JSONArray myArray = new JSONArray(response);

        for(int i = 0; i < myArray.length(); i++)
        {
            JSONObject jObj = myArray.getJSONObject(i);
            String category = jObj.getString("category");
            Log.d("category", category);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Maybe you can edit your answer to improve the code format, to be a better answer
why "Try this??" A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.What is meaning of repeating accepting answer!! Please add some description to make others understand. happy coding :)
0

I couldn't figure out how to use the JsonArrayRequest so i've used the StringRequest mentioned in my question and parsed the response like this:

@Override
            public void onResponse(String response) {
//              Log.d("productTAG", response);
                try {
                    JSONArray myArray = new JSONArray(response);

                    for(int i = 0; i < myArray.length(); i++)
                    {
                        JSONObject jObj = myArray.getJSONObject(i);
                        String category = jObj.getString("category");
                        Log.d("category", category);

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


This worked for me, also thanks @Peter P for your help!

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.