0

I'm trying to get a big JSON from a backend using Volley. I'm using a JsonObjectRequest and a singleton Volley request queue to do it. This my source:

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
                URL, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                apiResponse.response(response, null);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {    
                apiResponse.response(null, error);
            }
        });

        mRequestQueue.add(request);

But always I get this Exception:

java.lang.OutOfMemoryError
        at java.util.HashMap.makeTable(HashMap.java:569)
        at java.util.HashMap.doubleCapacity(HashMap.java:589)
        at java.util.HashMap.put(HashMap.java:419)
        at org.json.JSONObject.put(JSONObject.java:263)
        at org.json.JSONTokener.readObject(JSONTokener.java:385)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONTokener.readArray(JSONTokener.java:430)
        at org.json.JSONTokener.nextValue(JSONTokener.java:103)
        at org.json.JSONTokener.readObject(JSONTokener.java:385)
        at org.json.JSONTokener.nextValue(JSONTokener.java:100)
        at org.json.JSONObject.<init>(JSONObject.java:155)
        at org.json.JSONObject.<init>(JSONObject.java:172)
        at com.android.volley.toolbox.JsonObjectRequest.parseNetworkResponse(JsonObjectRequest.java:103)
        at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)

Do you have any idea why it's crashing? Could it be related to the big size of my JSON response?

Alex
  • 6,957
  • 12
  • 42
  • 48
  • [http://stackoverflow.com/questions/25868608/volley-framewok-request-keeps-objects-in-memory](http://stackoverflow.com/questions/25868608/volley-framewok-request-keeps-objects-in-memory) – M D Jul 11 '15 at 12:03
  • [https://groups.google.com/forum/#!topic/volley-users/pDBxx-r-YG0](https://groups.google.com/forum/#!topic/volley-users/pDBxx-r-YG0) – M D Jul 11 '15 at 12:07
  • to parsing the huge amount of data just use Gson library, – balu b Jul 11 '15 at 12:17
  • 1
    Volley allocates byte buffers as large as the response. If your responses take up a few megabytes, you can get OOM because of Volley. For big responses you should avoid volley and use another library instead, combined with a pull parser like Gson. If your responses are not that big, you probably have memory leaks or too large caches which take up too much memory in your app. – BladeCoder Jul 11 '15 at 12:29
  • Thanks for your response @BladeCoder . Do you have any suggestion as an alternative to Volley? – Alex Jul 12 '15 at 11:43
  • You could use OkHttp or the simple HttpURLConnection. For threading you can use RxJava or Android Loaders. – BladeCoder Jul 12 '15 at 13:58

1 Answers1

0

to parse huge amount of json data, use Gson library;

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

balu b
  • 282
  • 2
  • 7
  • Thanks for your answer, but the problem is the onResponse method is never called and I can't get the data. – Alex Jul 11 '15 at 12:22