我在 android 设备上使用肥皂服务,作为响应,我从服务器获得 7mb 文件。这会导致我的应用程序在将输入流转换为字符串时因内存不足错误而崩溃。MemoryAnalyzer 显示内存已分配给 StreamBuilder。处理如此大的反应的最佳方法是什么?
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();
}