I am using org.apache.http.HttpClient to get a response from an URL, and convert the HttpEntity response to a string using EntityUtils.toString().
However, when the data returned is huge, sometimes it throws an OutOfMemory error. This makes sense, as the heap space can't hold the entire response string in memory.
My question is how can I convert the HttpEntity response from HttpClient to a string and decrease the chances of these OutOfMemoryErrors (assuming I can't increase the heap size)?
I'd prefer the the alternative way to be as efficient as EntityUtils.toString() or close to as efficient.
The exception:
java.lang.OutOfMemoryError: Java heap space
at org.apache.http.util.CharArrayBuffer.<init>(CharArrayBuffer.java:59)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:230)
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:273)
This is my code:
import org.apache.http.util.EntityUtils;
import org.apache.http.client.*;
url = "http://test.com/test"
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity); //alternative???