0

I am working on http get request on android.

I receive a huge amount of json data from server, a string is unable to handle or store that data and getting OutOfMemory Exception.

Then I tried to save it in arrayList which can store a maximum of Integer.Maximum value. but it is getting OutOfMemory exception while storing at ~8970 location.

Here is my link which has json data.

http://ec2-50-19-105-251.compute-1.amazonaws.com/ad/Upload/getitemlist10122013035042.txt

here is my code:

ArrayList<String> newarr = new ArrayList<String>();
    try {

        URL url = new URL(urlFilePath);
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

        urlConnection.setRequestMethod("GET");
        // urlConnection.setDoOutput(true);

        // connect
        urlConnection.connect();

        // Stream used for reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        // create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        int check;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            String decoded = new String(buffer, 0, bufferLength);
            newarr.add(decoded);    // OutOfMemory Exception.   
        }

        fileOutput.close();
        buffer = null;
        inputStream.close();
        String path = file.getAbsolutePath();
        return path;
    } catch (final MalformedURLException e) {
        e.printStackTrace();
        return "";
    } catch (final IOException e) {
        e.printStackTrace();
        return "";
    } catch (final Exception e) {
        System.out.println("EXCEPTION:: " + e.getMessage());
        e.printStackTrace();
        return "";
    }
user2085965
  • 393
  • 2
  • 13
  • 33

1 Answers1

0

You need to process the stream directly instead of storing it as a String. Look at the Gson Stream Reader as an example:

public List<Message> readJsonStream(InputStream in) throws IOException {
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    List<Message> messages = new ArrayList<Message>();
    reader.beginArray();
    while (reader.hasNext()) {
        Message message = gson.fromJson(reader, Message.class);
        messages.add(message);
    }
    reader.endArray();
    reader.close();
    return messages;
}

You can think of the List here as the parsed result. You could also use the list from whatever ListAdapter you are using if you want to display it.

David S.
  • 6,567
  • 1
  • 25
  • 45