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?

jack_the_beast
  • 1,838
  • 4
  • 34
  • 67
  • 1
    Please Share output of `Log.d("JSON", "resp: " +response);` And refer http://stackoverflow.com/questions/8740381/getting-a-jsonexception-end-of-input-at-character-0 – Giru Bhai Apr 14 '15 at 12:42
  • here is the output: `resp:[{"roundid":4152,"numberofplayers":1,"dateevent":"2015-04-13 19:45:32.121124+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":4154,"numberofplayers":1,"dateevent":"2015-04-13 20:16:08.845409+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":4155,"numberofplayers":1,"dateevent":"2015-04-13 20:18:22.002411+02","playernames":"cat","turn":1,"codedboard":""}]` – jack_the_beast Apr 14 '15 at 12:49

2 Answers2

0

Total shot in the dark, but I had similar occur on an RSS parser. It turns out the URL I was using was HTTP, but redirected to HTTPS and I was using an HttpURLConnection instead of HttpsURLConnection.

Though, I wasn't using Android Volley so YMMV.

MCLLC
  • 496
  • 4
  • 12
0

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

jack_the_beast
  • 1,838
  • 4
  • 34
  • 67