Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

I would recommend the second method IF you do not want to display the amount of data downloaded/transferred . As the object is read as a whole and since the size of your JSON string is comparable to 1M, it will take some time to download. At that time you can, atmost, put up a text for the user saying downloading... You cannot notify the user the amount downloaded.

But if you want to display the amount of data downloaded, use the first method that you gave. Where the you read the data from the server in parts. You can update the UI, with the amount downloaded. For eg 25 % downloaded...

char[] charBuffer = new char[bufferSize];
    int _postion = 0;
    int i=0;
    while ((_postion=responseInputStream.read(charBuffer)) > -1) {

       //((i*buffer_size)/content_length) * 100  % completed..

        i++;
        }


So, I would say the seconds method is better.


BTW Did you consider this?

ObjectInputStream in = new InputStreamReader(conn.getInputStream(),"UTF-8");
if(resposeCode==200)
{
 String from_server=(String)  in.readObject();
}

Reading the input String as an object. Any object whoe class implements serializable can be passed using ObjectOutputStream and received using ObjectInputStream()

I Think First one is good

Because, in First that will reading your response in char to char method .

Where , Second that will try to read whole response object or as key Filed of Object.

So ,As i think and as per my knowledge First is Better to camper with second.If anyone want to edit then it will truly appreciated.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top