Search code examples
androidandroid-volleyjsonexception

Android Volley. JsonArray JsonException end of input at character 0


I'm trying to retrieve a jsonarray from a url using Volley. the problem is that I get

JsonException end of input at character 0

the code is the following:

JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, openMatchesUrl,
                 new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d("JSON", response.toString());
                        try {
                            for (int i = 0; i < response.length(); i++) {

                                //do stuff
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(getApplicationContext(),
                                "onErrorResponse ongoing: "+error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }){     
        @Override
        protected Map<String, String> getParams() 
        {  
               //build params 
        }
    };
    // Add the request to the RequestQueue.
    queue.add(req);

I was thinking that the problem was in wrong parameters. but I tried with a simple string request:

StringRequest req = new StringRequest(Request.Method.POST, openMatchesUrl,
                new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
                 Log.d("JSON", "resp: " +response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("JSON", error.toString());
        }
    }){     
            @Override
            protected Map<String, String> getParams() 
            {  
                    //build params 
            }
        };

and it's actually returning the json correctly. for example:

[{"roundid":4152,"numberofplayers":1,"dateevent":"2015-04-13 19:45:32.121124+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":415‌​4,"numberofplayers":1,"dateevent":"2015-04-13 20:16:08.845409+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":415‌​5,"numberofplayers":1,"dateevent":"2015-04-13 20:18:22.002411+02","playernames":"cat","turn":1,"codedboard":""}]

what's the problem here?


Solution

  • I finally solved this, the problem here is that for some reason JSonArrayRequest was not taking the POST parameters.

    So I just manually appended the parameters to the url