Apache.commons.io.IOUtils error from memory

I use the following code to get a string representation of the server response. The problem is that it throws an exception in memory when it tries to pass the response toString() . This code works on Android.

 String resp = IOUtils.toString(resEntity.getContent(), "UTF-8"); HttpGet httpGet = new HttpGet(url); httpGet.setHeader("Content-Type", "application/json"); httpGet.setHeader("Accept", "JSON"); HttpResponse httpResponse = httpclient.execute(httpGet); HttpEntity resEntity = httpResponse.getEntity(); if (resEntity != null) { //String resp = EntityUtils.toString(resEntity); String resp = IOUtils.toString(resEntity.getContent(), "UTF-8"); // exception here Log.i(TAG, resp); // ........ // gosn parser code here // ........ } 

Stacktrace element:

 java.lang.OutOfMemoryError: (Heap Size=47395KB, Allocated=36544KB) 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 org.apache.commons.io.output.StringBuilderWriter.write(SourceFile:138) at org.apache.commons.io.IOUtils.copyLarge(SourceFile:2002) at org.apache.commons.io.IOUtils.copyLarge(SourceFile:1980) at org.apache.commons.io.IOUtils.copy(SourceFile:1957) at org.apache.commons.io.IOUtils.copy(SourceFile:1907) at org.apache.commons.io.IOUtils.toString(SourceFile:778) at org.apache.commons.io.IOUtils.toString(SourceFile:803) 
+4
source share

Source: https://habr.com/ru/post/1497816/


All Articles