我正在使用以下代码来获取服务器响应的字符串表示形式。问题是它在尝试传递响应toString()
时抛出了内存不足的异常。此代码在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
// ........
}
堆栈跟踪是:
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)