0

I am getting the out of memory exception when I am trying to convert InputStream to String. Here my json response is too large near about 4mb and it contains some special characters also some thing like this:

·&nbsp; &nbsp; &nbsp; &nbsp;  Scope of Work : Integrate RBS6601, DUL, Data test, Create COP\r<br />\r<br /> \r<br />\r<br />·&nbsp; &nbsp;

This issue is not reproducible in all Android devices in low end phones I am getting this issue. How can I overcome this issue, can anybody help me on this..

I am using the below logic to convert it:

InputStream is = httpResponse.getEntity().getContent();
String output = null;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
//store  response in buffer
while ((output = br.readLine()) != null) {
    stringBuffer.append(output);
}
stringBuffer.trimToSize();

getting the below error:

java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
at java.lang.StringBuilder.append(StringBuilder.java:271)
at java.io.BufferedReader.readLine(BufferedReader.java:417)
at com.ldoapps.activitymanager.services.DownloadData.postData(DownloadData.java:359)
at com.ldoapps.activitymanager.services.DownloadData.doInBackground(DownloadData.java:239)
at com.ldoapps.activitymanager.services.DownloadData.onHandleIntent(DownloadData.java:220)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.os.HandlerThread.run(HandlerThread.java:60)
ByteHamster
  • 4,884
  • 9
  • 38
  • 53
user448250
  • 1,614
  • 2
  • 13
  • 15
  • What do you intend to do with the 4MB string? Storing something that large in memory is undesirable and likely unnecessary. – pathfinderelite May 06 '15 at 22:25
  • It wont be always 4mb but that specific user had that much data, because in my app there is an option to user where he can enter the comments there user enetered large amount of data, like this he did so many times so the data size got increased. – user448250 May 06 '15 at 23:15
  • How can I handle this kind of situation.? – user448250 May 07 '15 at 00:01
  • Why do you need it in memory? Networking, files, parsers, they all support `InputStream` – StenSoft May 07 '15 at 00:12
  • I am making a httppost request to server which returns me a inputstream then i am converting that inputstream to a string which is a json string response, after i will parse that json string and store it in db. – user448250 May 07 '15 at 01:06
  • I think this answer have your solution http://stackoverflow.com/questions/11104171/android-parsing-large-json-file – sabbir May 07 '15 at 02:34

0 Answers0