title: Android HttpUrlConnection how to read response into pieces
body: <p>actually I need a little more information about how to read response from <code>HttpUrlConnection</code> class in Android SDK. I'm trying to read a response from web server,but when it's too big my applications is throwin an <code>OutOfMemoryException</code>.  So any source/help/suggestions on how to read the whole response into pieces is welcomed.</p>
  
  <p>As I made a research I found out that I should set something like this : <code>((HttpURLConnection) connection).setChunkedStreamingMode(1024);</code> But my problem is that I don't know how to read this chuncked stream. So I'll be very happy if someone can guide me through the right way.</p>
  
  <p>Thanks! </p>
  
  <p>Sample Code : </p>
  
  <pre><code>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;
  
              packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
  
              clientApiVersion = packageInfo.versionName;
              Log.w("client_api_ver","client_api_ver : "+clientApiVersion);
  
              long timestamp = System.currentTimeMillis()/1000;
              String timeStamp = Long.toString(timestamp);
  
  
              String url = "http://www.rpc.shutdown.com";
              String charset = "UTF-8";
              String usernameHash = hashUser(username,password);
              String passwordHash = hashPass(username,password);
  
  
              String query = String.format("username_hash=%s&amp;password_hash=%s&amp;new_auth_data=%s&amp;debug_data=%s&amp;client_api_ver=%s&amp;set_locale=%s&amp;timestamp=%s&amp;"+
                       "device_os_type=%s&amp;mobile_imei=%s&amp;device_sync_type=%s&amp;device_identification_string=%s&amp;device_identificator=%s&amp;device_resolution=%s", 
                       URLEncoder.encode(usernameHash, charset), 
                       URLEncoder.encode(passwordHash, charset),
                       URLEncoder.encode("1", charset),
                       URLEncoder.encode("1", charset),
                       URLEncoder.encode(clientApiVersion, charset),
                       URLEncoder.encode(locale, charset),
                       URLEncoder.encode(timeStamp, charset),
                       URLEncoder.encode(version, charset),
                       URLEncoder.encode(deviceId, charset),
                       URLEncoder.encode("14", charset),
                       URLEncoder.encode(version, charset),
                       URLEncoder.encode(deviceId, charset),
                       URLEncoder.encode(resolution, charset));
  
              HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
              connection.setDoOutput(true); // Triggers POST.
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Connection", "Keep-Alive");
              connection.setRequestProperty("Charset", charset);
              connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
              OutputStream output = null;
              try {
                   output = connection.getOutputStream();
                   output.write(query.getBytes(charset));
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                   if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
              }
  
              int status = ((HttpURLConnection) connection).getResponseCode();
              Log.i("","Status : "+status);
  
              for (Entry&lt;String, List&lt;String&gt;&gt; header : connection.getHeaderFields().entrySet()) {
                  Log.i("Headers","Headers : "+header.getKey() + "=" + header.getValue());
              }
  
              InputStream response = connection.getInputStream();
              Log.i("","Response : "+response.toString());
              int bytesRead = -1;
              byte[] buffer = new byte[8*1024];
              while ((bytesRead = response.read(buffer)) &gt;= 0) {
                String line = new String(buffer, "UTF-8");
                Log.i("","line : "+line);
                handleDataFromSync(buffer);
              }
  </code></pre>