Pepa Zapletal
Pepa Zapletal

Reputation: 2979

Android - HTTP GET Request huge size of JSON response

I have a big JSON input (download the file) API and I don´t know how to parse this data. I need:

  1. Save this data (entire JSON input) to text file or database. What is the best way for this?
  2. Load this data from text file or database and create JSONArray from JSON tag "list" (first tag)

The solution should be fast and support Android 2.3. What you have recomend for this? Any ideas?

My code:

        HttpClient httpClient = new DefaultHttpClient();

        HttpGet httpGet = new HttpGet(urls[0]);

        HttpResponse httpResponse;

        httpResponse = httpClient.execute(httpGet);

        HttpEntity httpEntity = httpResponse.getEntity();

            ... and what next ?...

FYI:

EntityUtils throws OutOfMemoryException

EDIT:

I try to save data to file like this:

            InputStream inputStream = httpEntity.getContent();

            FileOutputStream output = new FileOutputStream(Globals.fileNews);
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int len = 0;

            while ((len = inputStream.read(buffer)) != -1) {
                output.write(buffer, 0, len);
            }

And it´s OK. I load data:

            FileInputStream fis = null;
            StringBuffer fileContent = new StringBuffer("");

            fis = new FileInputStream(Globals.fileNews);

            byte[] buffer = new byte[1024];

            while (fis.read(buffer) != -1) {
                fileContent.append(new String(buffer));
            }

But how convert StringBuffer to JSONObject? fileContent.ToString() is not ideal, sometimes I get OutOfMemoryException.

Upvotes: 0

Views: 3251

Answers (1)

schlingel
schlingel

Reputation: 8575

First of all: Dispose the HttpClient. Google discourages it:

Unfortunately, Apache HTTP Client does not, which is one of the many reasons we discourage its use.

Source: developer.android.com

A good replacement is Google Volley. You have to build the JAR yourself but it just works like charm. I use for my setups Google Volley with OkHttp-Stack and GSON requests.

In your case you would write another Request which just writes the response out to the SD-card chunk by chunk. You don't buffer the string before! And some logic to open an input-stream from the file you wrote and give it to your JSON Deserializer. Jackson and GSON are able to handle streams out of the box.

Of course everything works with Android 2.3.

Don't, I repeat, don't try to dump the whole serialized stuff into a string or something. That's almost a OutOfMemoryException guarantee.

Upvotes: 1

Related Questions