Android HttpEntityUtils OutOfMemoryException

2019-02-28 23:04发布

问题:

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 activate some setting, there is no problem. But when I send request to download a binary data it's throwing me an OutOfMemoryException. Here is the code which I'm using :

public byte[] activateColl(int index) {
        createCancelProgressDialog("","Communcating with you...","Cancel");

        byte[] buffer = new byte[1024];
        try {
                httpclient = new DefaultHttpClient();
                httppost = new HttpPost("http://www.rpc.frbr.com");

                TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
                String deviceId = tm.getDeviceId();
                Log.w("device_identificator","device_identificator : "+deviceId);
                String resolution = Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getWidth())+ "x" +
                                             Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getHeight());
                Log.w("device_resolution","device_resolution : "+resolution);
                String version = "Android " + Build.VERSION.RELEASE;
                Log.w("device_os_type","device_os_type : "+version);
                Log.w("device_identification_string","device_identification_string : "+version);
                String locale = getResources().getConfiguration().locale.toString();
                Log.w("set_locale","set_locale : "+locale);
                String clientApiVersion = null;

                PackageManager pm = this.getPackageManager();
                PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
                clientApiVersion = packageInfo.versionName;
                Log.w("client_api_ver","client_api_ver : "+clientApiVersion);

                hash = getAuthHash();

                TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
                phoneNumber = tMgr.getLine1Number();
                Log.i("Phone","Phone Number : "+phoneNumber);

                Log.w("INDEX","INDEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX : "+Integer.toString(index));

                String timestampSQL = "SELECT dbTimestamp FROM users";
                Cursor cursor = systemDbHelper.executeSQLQuery(timestampSQL);
                if(cursor.getCount()==0){
                    Log.i("Cursor","TimeStamp Cursor Empty!");
                } else if(cursor.getCount()>0){
                    cursor.moveToFirst();
                    timeStamp = cursor.getString(cursor.getColumnIndex("dbTimestamp"));
                }


                postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("debug_data","1"));
                postParameters.add(new BasicNameValuePair("client_auth_hash", hash));
                postParameters.add(new BasicNameValuePair("timestamp", timeStamp));
                postParameters.add(new BasicNameValuePair("mobile_phone", phoneNumber));
                postParameters.add(new BasicNameValuePair("activate_collections",Integer.toString(index)));
                postParameters.add(new BasicNameValuePair("device_identificator", deviceId));
                postParameters.add(new BasicNameValuePair("device_resolution", resolution));

                httppost.setEntity(new UrlEncodedFormEntity(postParameters));

                HttpResponse response = httpclient.execute(httppost);
                Log.w("Response ","Status line : "+ response.getStatusLine().toString());

                buffer = EntityUtils.toByteArray(response.getEntity());

            } catch (ParseException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
                return buffer;
        }

Error from the LogCat:

10-17 17:53:00.198: ERROR/AndroidRuntime(15486): java.lang.OutOfMemoryError
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at org.apache.http.util.ByteArrayBuffer.expand(ByteArrayBuffer.java:57)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at org.apache.http.util.ByteArrayBuffer.append(ByteArrayBuffer.java:75)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at org.apache.http.util.EntityUtils.toByteArray(EntityUtils.java:80)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at com.stampii.stampii.synchronization.Synchronization.deactivateColl(Synchronization.java:843)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at com.stampii.stampii.synchronization.Synchronization.onCreate(Synchronization.java:116)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.app.ActivityThread.access$2300(ActivityThread.java:135)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.os.Looper.loop(Looper.java:144)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at android.app.ActivityThread.main(ActivityThread.java:4937)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at java.lang.reflect.Method.invokeNative(Native Method)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at java.lang.reflect.Method.invoke(Method.java:521)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-17 17:53:00.198: ERROR/AndroidRuntime(15486):     at dalvik.system.NativeStart.main(Native Method)

Any ideas how to fix that? I tried to increase my buffer's size to 50*1024,but still doesn't help.

EDIT: Error is throws on that line :

buffer = EntityUtils.toByteArray(response.getEntity());

回答1:

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


回答2:

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.