I’m using a soap service on android device and as response I get 7mb file from server. This causes my app to crash with out of memory error when converting input stream to string. MemoryAnalyzer shows that memory was allocated to StreamBuilder.
What is the best way to deal with such big responses?
HttpEntity entity = new StringEntity(soap.toString());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
if( r_entity != null ) {
result = inputStreamToString(r_entity.getContent());
}
...
//convert stream to string
public static String inputStreamToString(InputStream stream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
}
The most obvious answer is to use streams and parse the result as it comes. If it’s an XML file you’re parsing then SAX is probably the best avenue for you.