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

  • SEARCH
  • Home
  • 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 7783991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:00:28+00:00 2026-06-01T20:00:28+00:00

I need a little help with sending an HttpUrlConnection from my android application. Till

  • 0

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
  • 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-06-01T20:00:30+00:00Added an answer on June 1, 2026 at 8:00 pm

    Here is the way you can use HttpURLConnecion to make a connection to a web server :

            System.setProperty("http.keepAlive", "false");
            connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setDoOutput(true);
            connection.setConnectTimeout(5000); // miliseconds
            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 e) {
                        e.printStackTrace();
                    }
            }
    
            int status = ((HttpURLConnection) connection).getResponseCode();
            Log.d("", "Status : " + status);
    
            for (Entry<String, List<String>> header : connection
                    .getHeaderFields().entrySet()) {
                Log.d("Headers",
                        "Headers : " + header.getKey() + "="
                                + header.getValue());
            }
    
            InputStream response = new BufferedInputStream(
                    connection.getInputStream());
    
            int bytesRead = -1;
            byte[] buffer = new byte[30 * 1024];
            while ((bytesRead = response.read(buffer)) > 0 && stopThread) {
                byte[] buffer2 = new byte[bytesRead];
                System.arraycopy(buffer, 0, buffer2, 0, bytesRead);
                // buffer2 is you chunked response
            }
            connection.disconnect();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a little help on this subject. I have a Web application written
I need a little help getting a tar file to download from a website.
I need a little help here: I get a file from an HTML upload
I need a little help with this.. I need to take only the numbers
need a little help with this one. I have a form that I am
Need a little help with string formatting... I have a string like this: Bmw
I need a little help with updating my UI from Runnable/Handler every second. I'm
Need a little help here : I'm out of ideas now... Here's what I
I need little bit help related to android tabhost. I have 3 tabs and
I'm new to C# (asp.net) so i need little help. On this forum I

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.