0

I am doing a http Get request. I need to receive huge data, but getting OutOfMemory Exception while reading data.

My code:

public static String getData(String url) throws CustomException {
    // http post
    InputStream is = null;
    StringBuilder sb = null;
    String result = null;
    HttpGet httppost;
    HttpEntity entity;

    try {
        HttpClient httpclient = new DefaultHttpClient();
        httppost = new HttpGet(url);
        HttpResponse response = httpclient.execute(httppost);
        entity = response.getEntity();
        is = entity.getContent();
        Log.e("log_tag", "connection success ");
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection" + e.toString());
        throw new CustomException("Could not establish network connection");
    }

    // convert response to string
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c;
        byte buffer[] = new byte[1024];
        while ((c = is.read(buffer)) > -1)
            baos.write(buffer, 0, c); //**OutOfMemory Exception.**
        byte[] data = baos.toByteArray();

        is.close();
        result = new String(data, 0, data.length, "utf-8");

    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
        throw new CustomException("Error parsing the response");
    }

    return result;

}

URL which I pass to getData is : http://ec2-50-19-105-251.compute-1.amazonaws.com/ad/Upload/getitemlist09122013014749.txt

Please suggest me how to solve this.

2
  • there are so many records thats why.. i think you should install app in sdcard or phone storage.. Commented Dec 10, 2013 at 6:28
  • Dude, the url return lots of data. Don't add it to memory. Use file system. Commented Dec 10, 2013 at 6:28

2 Answers 2

2

The received file is large, you need to write the response to file instead of ByteArrayOutputStream, and then try to parse the result from file.

And if possible, the server should split the large file into small chunks.

Sign up to request clarification or add additional context in comments.

Comments

0

One possible solution is to implement pagination in fetching the result from server. After that you can write the data somewhere gradually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.