Android HttpEntityUtils OutOfMemoryException

孤街醉人 提交于 2019-12-02 07:13:00
HefferWolf

On Android you only have a limited heap size, which gets exhausted while you try to decode your entity. I think you need to use http chunking to send your data to the client (or something goes wrong and the EntityUtils think they need a much bigger array. The problem is a byte array which is to big not to small. Have a look at this posts:

  1. Detect application heap size in Android
  2. Android heap size on different phones/devices and OS versions

Ok, try this..

HttpResponse response = httpclient.execute(httppost);         
 // get response entity 
 HttpEntity entity = response.getEntity();          
 // convert entity response to string  
   if (entity != null) 
    {      
     InputStream is = entity.getContent();     
     // convert stream to string    
     result = convertStreamToString(is);      
     result = result.replace("\n", "");     

   } 

And for conversion of InputStream to String

public String convertStreamToString(InputStream inputStream)
{
 BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
 StringBuilder total = new StringBuilder();
 String line;
  while ((line = r.readLine()) != null) {
    total.append(line);
  }
return total.toString();
}

or

use Apache Commons library (org.apache.commons.io.IOUtils).

  String total = IOUtils.toString(inputStream);

or just write to temp file,

  HttpEntity.writeTo(OutputStream);

Ok as per user Philipp Reichart suggested, from package of org.apache.http.util;

EntityUtils.toString(); 

is the method which allow the encoding format with String Conversion.

and let me know still you get OutOfMemory Error.

Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!