简体   繁体   中英

What is the difference between entity.getContent() and EntityUtils.tostring()

I am just wondering for an android application

after establishing a http client and obtain a httpResponse there are two ways to obtain the data. the first one is response.getEntity().getContent() which returns a input stream and use a inputstreamreader to read the data and the second one is directly obtain the data using EntityUtils.toString(response.getEntity()).

Is there a difference? which way is preferred?

Thanks

response.getEntity().getContent() returns input stream - so you can read data as it coming from server - you do not need to wait util all data is received before start doing something with it(for example parsing data or rendering image). Also in some cases you can not(or by performance reasons do not want) to store all received data in memory at the same time - in this case you also should use this variant.

But in case when you are expecting relatively short string and will have no benefits from InputStream - you can use second variant ( EntityUtils.toString(response.getEntity()) ) without any drawbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM