URL url = new URL("http://pubapi.cryptsy.com/api.php?method=orderdatav2");
CryptsyCurrencyPairsReturn response = gson.fromJson(new InputStreamReader(url.openStream()), CryptsyCurrencyPairsReturn.class);
This results in an OutOfMemoryException for some of my users on older Android devices. How can I parse this large response without running out of memory?
I'd recommend looking at JsonReader (API 11+), or the other options as recommended here.
From my experience, you need to stream the JSON data.
And yes you can use google GSON to stream JSON data this is an example how to do it :
readItemsArray function :
API Model Class :
before I use the streaming API from google GSON I also got OOM error because the JSON data I got is very big data (many images and sounds in Base64 encoding) but with GSON streaming I can overcome that error because it reads the data per token not all at once. And for Jackson JSON library I think it also have streaming API and how to use it almost same with my implementation with google GSON. I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :)
Parsing large data in one-go is always tricky and troublesome. However Gson comes with some nice features to support that too.
You should look for
com.google.gson.stream.JsonReader
to perform json parsing using streams. This will allow you to parse the data in an incremental order while its being downloaded and will spare devices from OutOfMemory errors.For more info, read this.