Android HttpEntityUtils OutOfMemoryException

后端 未结 2 1843
清酒与你
清酒与你 2021-01-24 23:24

I\'m working on a project which connect to a web server and receive binary data. My problem is when I\'m downloading data from web server. If i send request to login or to activ

相关标签:
2条回答
  • 2021-01-25 00:02

    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
    0 讨论(0)
  • 2021-01-25 00:16

    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.

    0 讨论(0)
提交回复
热议问题