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());