I need a little help with sending an HttpUrlConnection from my android application. Till now I was doing this with a basic Http Client. But the problem is that when I receive a big stream from the server my applications crash with outofmemory exception. And that’s why I made a research and find out that HttpUrlConnection lets me to get the stream into a pieces. So can anybody help me a little bit with sending my params and getting the response from server?
The previous code that I was using is this :
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.rpc.your_nightmare.com");
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
String resolution = Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getWidth())+ "x" +
Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getHeight());
String version = "Android " + Build.VERSION.RELEASE;
String locale = getResources().getConfiguration().locale.toString();
String clientApiVersion = null;
PackageManager pm = this.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
clientApiVersion = packageInfo.versionName;
hash = getAuthHash();
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"));
}
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
phoneNumber = tMgr.getLine1Number();
Log.i("Phone","Phone Number : "+phoneNumber);
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("deactivate_collections",Integer.toString(index)));
postParameters.add(new BasicNameValuePair("client_api_ver", clientApiVersion));
postParameters.add(new BasicNameValuePair("set_locale", locale));
postParameters.add(new BasicNameValuePair("device_os_type", version));
postParameters.add(new BasicNameValuePair("device_sync_type", "14"));
postParameters.add(new BasicNameValuePair("device_identification_string", version));
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());
HttpEntity entity = response.getEntity();
InputStream stream2 = entity.getContent();
int nRead;
byte[] data = new byte[8*1024];
while ((nRead = stream2.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
and than processing it like this :
InputStream stream = new ByteArrayInputStream(buffer, 0, temp.length);
Log.i("Temp","Temp : "+temp.length);
Log.i("index","index : "+index);
responseBody = convertStreamToString(stream);
Log.i("responseBody","responseBody : "+responseBody);
//calculations
Here is the way you can use
HttpURLConnecionto make a connection to a web server :