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.

Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
ffs
  • 148
  • 1
  • 2
  • 11
  • http://stackoverflow.com/questions/8740381/getting-a-jsonexception-end-of-input-at-character-0 – Peter P Jun 28 '15 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? – ffs Jun 28 '15 at 14:35
  • How does the Json returned by the response look like? – Peter P Jun 28 '15 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 "}] – ffs Jun 28 '15 at 15:13
  • @ffs You should edit your question with the Json rather than have it in a comment. – user4989692 Jun 28 '15 at 15:55
  • thanks for the observation. i`ve edited my question. – ffs Jun 28 '15 at 16:09
  • What's the content type on the request? HAve you tried adding `header('Content-Type: application/json');` before outputting the data? – TMH Jun 29 '15 at 12:11

2 Answers2

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();
    }
}
Bert Peters
  • 1,505
  • 13
  • 30
  • Maybe you can edit your answer to improve the code format, to be a better answer – Kalamarico Sep 13 '17 at 11:26
  • 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 :) – Rucha Bhatt Joshi Sep 15 '17 at 11:10
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!

ffs
  • 148
  • 1
  • 2
  • 11