Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:20:31+00:00 2026-05-26T08:20:31+00:00

actually I need a little more information about how to read response from HttpUrlConnection

  • 0

actually I need a little more information about how to read response from HttpUrlConnection 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 OutOfMemoryException. So any source/help/suggestions on how to read the whole response into pieces is welcomed.

As I made a research I found out that I should set something like this : ((HttpURLConnection) connection).setChunkedStreamingMode(1024); 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.

Thanks!

Sample 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&password_hash=%s&new_auth_data=%s&debug_data=%s&client_api_ver=%s&set_locale=%s&timestamp=%s&"+
                     "device_os_type=%s&mobile_imei=%s&device_sync_type=%s&device_identification_string=%s&device_identificator=%s&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<String, List<String>> 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)) >= 0) {
              String line = new String(buffer, "UTF-8");
              Log.i("","line : "+line);
              handleDataFromSync(buffer);
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:20:32+00:00Added an answer on May 26, 2026 at 8:20 am

    Simply allocate a byte buffer that can hold a small amount of data and read from the input stream into this buffer (using the read method). Something like:

    InputStream is = connection.getInputStream();
    
    int bytesRead = -1;
    byte[] buffer = new byte[1024];
    while ((bytesRead = is.read(buffer)) >= 0) {
      // process the buffer, "bytesRead" have been read, no more, no less
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Actually in one of my project i need to read images from remote server
Let's say, hypothetically (read: I don't think I actually need this, but I am
I am trying to understand simple snaplet construction. Also, when do I actually need
I need a help from ur side. Actually I need to call a method
Hi Programmers! Actually i need the distinct values from the List after adding some
Actually I have to upload pdf files and need to read on my website
I'm getting pretty big input string into my method, but what I actually need
Actually We are doing thesis work where we need to make 10 voip phones
I need a table where rows are actually 2 rows tables, a nested table
I need to getting the client ip address using javascript.Actually,our connection provider ip is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.