Im using using following Method to catch Data from a webapi:
public static String sendRequest(String requestURL, String data)
throws IOException {
URL url = new URL(requestURL + "?" + data);
URLConnection conn = url.openConnection();
conn.setReadTimeout(10000);
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
StringBuilder answerBuilder = new StringBuilder("");
try {
while ((inputLine = in.readLine()) != null)
answerBuilder.append(inputLine);
in.close();
} catch (Exception e) {
}
return answerBuilder.toString();
}
With some requests, this leads to a OutOfMemoryError caused by a too small HeapSize:
(...)Caused by: java.lang.OutOfMemoryError: (Heap Size=17927KB, Allocated=14191KB, Bitmap Size=2589KB)
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:95)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:132)
at java.lang.StringBuilder.append(StringBuilder.java:272)
at java.io.BufferedReader.readLine(BufferedReader.java:423)
at com.elophant.utils.HTTPUtils.sendRequest(HTTPUtils.java:23)
at (..)
I already swapped from normal String operations like String answer += inputLine
to a StringBuilder but this didnt help. How can i solve this Problem? Increasing maximum heap size via export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m"
isnt an option as its an android app.