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 8808863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:43:42+00:00 2026-06-14T02:43:42+00:00

We usually get data from server response in android development. /* * get server

  • 0

We usually get data from server response in android development.

/*
* get server response inputStream
*/
InputStream responseInputStream;

Solution1: get response string by multiple read.

      /*
       * get server response string
      */
    StringBuffer responseString = new StringBuffer();
    responseInputStream = new InputStreamReader(conn.getInputStream(),"UTF-8");

    char[] charBuffer = new char[bufferSize];
    int _postion = 0;
    while ((_postion=responseInputStream.read(charBuffer)) > -1) {
        responseString.append(charBuffer,0,_postion);
        }
    responseInputStream.close();

Solution2: get response only one read.

String responseString = null;
int content_length=1024;
// we can get content length from response header, here assign 1024 for simple.
responseInputStream = new InputStreamReader(conn.getInputStream(),"UTF-8");

char[] charBuffer = new char[content_length];
int _postion = 0;
int position = responseInputStream.read(charBuffer)
if(position>-1){
   responseString = new String(charBuffer,0,position );
 }
responseInputStream.close();

Which solution has better performance? why?

Notes: server response json format data that less than 1M bytes.

  • 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-14T02:43:43+00:00Added an answer on June 14, 2026 at 2:43 am

    Why you’re reinventing a wheel? 😉

    If you’re using HttpClient then just use EntityUtils.toString(...).
    I guess you’re using HttpURLConnection. Then look at EntityUtils.toString(...) from Apache HttpClient – source code. Your first approach is similar to it.

    BTW, the second code is worse because:

    new String(charBuffer,0,position ) runs garbage collector

    In both and even in EntityUtils:

    int content_length = 1024; in most cases 8192 is default for socket buffer, so your code might run while loop 8 times more often than it could.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get from another class string that must be converted to char. It usually
Usually when I get POST data it's send from a HTML form and the
I am not getting the correct output from the server. The response I get
I would like to export data from a SQL Server pull (usually I use
I use JSONP on a client to get data from a server using a
Usually, in order to launch a new Activity and get its result, from an
I have an SQL query I get from a configuration file, this query usually
I'm having a problem reading and processing data from my server. I've spent hours
Currently my application is getting data from remote server through web services. But some
Suppose I get data from a service (that I can't control) as: public class

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.